Why can't I set label fonts directly throught label.font = fontfile?

Godot Version

v4.2.1

Question

I tried to set label properties like font and font size directly through adding the values after ‘label.property =’ but somehow it doesn’t work (and getting this error: “Invalid set index ‘font’ (on base: ‘Label’) with value of type ‘FontFile’.”). Then I managed to get it work through set() method. But I’m still curious, why is the first approach, which seems very intuitive to me, doesn’t work? I looked up in the documentation and found ‘font’ property does exist for labels. So strange! Then what type of variables is supported by the font property if not fontfile?

What’s more strange is that font_size property doesn’t support ints…

Because a Control may have multiple fonts or font sizes (like a RichTextLabel for example) so that information is part of a Theme (fonts, font sizes, styleboxes, colors, icons and constants) and to override them you need to use any of the Control.add_theme_<type>_override() functions.

To know which properties you can modify for each Control they are listed in the Theme Properties section of each Control documentation page. You can also see them in the inspector under Theme Overrides

For a Label you can see which theme properties you can change here Label — Godot Engine (stable) documentation in English and to change them it should be label.add_theme_font_override("font", my_font) and label.add_theme_font_size_override("font_size", my_size)

1 Like

Font can applied to label using two method.

  1. Label setting property
  2. Theme property
    Both are resource
    1=
var l_s = LabelSetting.new()
label.set_label_setting(l_s)
l_s.font = load("font_file")

2=

var font_file = load("font_file_path")
label.add_theme_font_override("font", "font_file")

remember 1 method will override 2 if both area being used

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.