2
\$\begingroup\$

Trying to understand how theme lookup works in Godot 4.2. Suppose a Control node script has the following two lines in it:

print(get_theme_font_size("normal_font_size", "RichTextLabel"))
print(theme.get_font_size("normal_font_size", "RichTextLabel"))

Can it ever happen that these two calls print different output? And if so, how?

\$\endgroup\$
1
  • \$\begingroup\$ Are you asking from a strictly theoretical stand point or do you have a project where the example code is given different output? If it's the later, editing to describe how the results differ & how you set up your theme will allow others to directly troubleshoot your problem rather than guessing things that could change the results but don't apply to your situation. \$\endgroup\$
    – Pikalek
    Commented May 27 at 17:11

1 Answer 1

4
\$\begingroup\$

These are, indeed, different.


First of all, your Control might not have a Theme, and thus the line:

print(theme.get_font_size("normal_font_size", "RichTextLabel"))

Is an error (trying to call a method on a null value).


But more importantly, using get_theme_font_size et.al. will have in consideration the theme overrides that might be set on the Control, while accessing the theme directly will not.

Note that different Control classes offer different theme overrides.

And yes, theme overrides are available even without a Theme.


Addendum:

It is worth noting that it is intended that you set the Theme at the root of your UI, and all children Controls will use that same Theme, so you do not have to set the theme of every Control.

To be clear, the theme property is the Theme explicitly set on a Control. But a Theme might be applied on a Control because it is set on its theme property or on the theme property of one of its ancestors on the scene tree.

On the same logic, get_theme_font_size and similar methods will pick up the Theme that is applied to the Control, which might or might be set on the theme on the same Control or an ancestor.

By the way, theme overrides are specific of the Control. They do not propagate on the same manner.

\$\endgroup\$
2
  • \$\begingroup\$ All good points. I wonder: What is the best way to handle theme switching? E.g. when an app offers several themes the user can select from. The selection should persist through scene switches as well. Is this worth a separate question? \$\endgroup\$
    – shiroko
    Commented May 30 at 12:44
  • \$\begingroup\$ @Jakub When the theme changed Godot sends a NOTIFICATION_THEME_CHANGED which the Controls use to update their appearance. You can also react to the notification using _notification, for example if you need to adjust the size of a Control or something like that. \$\endgroup\$
    – Theraot
    Commented May 30 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .