0 votes

I would like to change font size of a theme. Looking through the documentation I can figure out the gets, but don't see an applicable sets to adjust a font (and thus its size) during runtime.

End state is having a player be able to adjust font size for easier viewing. I could do this by scalers, but seems to be a clunky way (scale then adjust positions and offsets, etc.).

There have been a few answers to similar questions, but those seem to only apply to earlier releases.

Thanks in advance.

asked Mar 22, 2019 in Engine by CalmTurtle (142 points)

1 Answer

0 votes
Best answer

Font size is not a property of the theme, it's a property of Font. Or more specifically, DynamicFont. So to change the font size of all your controls, you have to get the font (from the theme, from the control, or just load it) and set its size property.

If you want this to be applied only to specific controls, you may want to make another version of that font (but using the same DynamicFontData), or make a duplicate in code, set its size and add a font override to the control. In code, that's done with add_font_override().

In regard to the above, if a player wants to change fonts size of the game, you'd have to get all dynamic fonts in your project and set their size.

Note that if you did use multiple fonts in your project, you may want to keep them loaded when the game runs by maintaining a reference to them, so the change will persist. The reason is, in Godot, resources that are not referenced by anything are unloaded, which would make the size override to revert to what it was in the resource file. However if you have only one main font referenced in your Theme, you won't need this.

answered Mar 22, 2019 by Zylann (26,139 points)
selected Mar 24, 2019 by CalmTurtle

Thanks for the note. Using the above I ended up with the following code that worked out for me:

var font = $YourControlNode.get_font("string_name", "")
font.size = 20
$YourControlNode.add_font_override("string_name", font)

Note: the value of string_name does not have an effect that I can see.

The string name has no effect on most controls probably, but I think it's there for controls that may render text using multiple fonts.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.