The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+2 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.

in Engine by (149 points)

1 Answer

+2 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.

by (29,360 points)
selected by

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.

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

add_font_override() is not needed if you only want to change font size of $YourControlNode

Thanks for the hint! In c#:

var fontForExplanation = rtlExample.GetFont("normal_font","");
(fontForExplanation as DynamicFont).Size = 24; 

GetFont() returns Font which is forefather of DynamicFont and BitmapFont

Thanks for this answer in C#, it worked for me. I could not find any documentation.

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.