Daniel Paczuski Bak
Daniel Paczuski Bak

Reputation: 4088

godot: load dynamic font at runtime

I am trying to draw_string at runtime using a dynamic font.

The font is saved to the following location: res://assets/fonts/dynamicfont.tres

Here is my code:

func _draw():

    var font = DynamicFont.new()
    font.size = 32
    font.set_font_data(load("res://assets/fonts/dynamicfont.tres"))



    draw_string(font, Vector2(45, 45), "1 2 3 4", Color(0, 0, 0))

This does not draw the string, it appears that the font is just not being loaded.

I see a lot of tutorials saying how to generate a dynamic font, but I cannot find a single one describing how to load it at runtime and use it. Is this possible?

Upvotes: 0

Views: 998

Answers (1)

Bugfish
Bugfish

Reputation: 1729

I assume you saved a dynamic font there. So your tres is not the font_data, but the complete font itself. You should not load resources in the draw function, which will not work properly.

Initialize the font in the ready function and then use it in the draw:

var font

func _ready():
    font = load("res://assets/fonts/dynamicfont.tres")
    font.size = 32

func _draw():
    draw_string(font, Vector2(45,45), "1 2 3 4")

if you want to change the font, do it outside of the draw function and then call update() to force a redraw

Upvotes: 0

Related Questions