Changing the default font of a theme by code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Calinou
:warning: Old Version Published before Godot 3 was released.

I’m looking for a way to change the default font of a theme by code. This would be used to implement font scaling depending on the window size.

I’ve found how to change the custom font of a label already:

get_node("MyLabel").set("custom_fonts/font", my_font_data)
:bust_in_silhouette: Reply From: jackmakesthings

You can do something like this:

var theme = get_theme()
var newfont = load('assets/fonts/different-font.fnt')
theme.set_default_font(newfont)

I haven’t confirmed whether this works on controls that just inherit a theme; if it doesn’t, just call get_theme() on the themed parent control instead.

(Also, if all you’re changing is the size, you can probably refine this to use DynamicFont and DynamicFontData.)