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

0 votes

I'm trying to write some text without using labels on screen with each of the sprites. My scene is like this:

    |-> KinematicBody2D

The Sprite has the 50x50 texture (a rounded piece) and I want to add text over it using _draw():

func _draw():
    var _font = load("res://FreePixel.ttf")

    var dynamic_font =
    dynamic_font.font_data = load("res://FreePixel.ttf")
    dynamic_font.size = 30
    draw_string(dynamic_font, Vector2(12, 32), 'aC',

All I get is this:

enter image description here

Both characters ('aC') are just a red box. If I use the _font instead of dynamic_font resource I get nothing. TTF is here.

What's wrong?

in Engine by (38 points)

1 Answer

+2 votes
Best answer

It's a bit unintuitive, but the way you wrote your _draw function will not work.
Resources are reference-counted by the places where they are in use.
You are loading the font, but also you don't store it anywhere. So it gets unloaded when the function finishes... which means Godot has to do the effort of reading it from disk and uploading it to the graphics card every frame.

And as it turns out, the renderer does not actually draw things immediately. Drawing commands are all executed in one go later, in the rendering thread. But at this moment, the font will have been unloaded because you didn't hold it anywhere. So Godot just draws quads because the font is no longer available.

Finally, you even load the font twice. Once in _font, once in dynamic_font.font_data, this is not necessary.

You should load and create the font in _ready(), store it in a member variable, and use that variable in _draw():

var _dynamic_font

func _ready():
    _dynamic_font =
    _dynamic_font.font_data = load("res://FreePixel.ttf")
    _dynamic_font.size = 30

func _draw():
    draw_string(_dynamic_font, Vector2(12, 32), 'aC',

Also, if you want to save yourself some hassle about file paths and renaming, you could even create the DynamicFont resource as a file, and use an export(DynamicFont) var _dynamic_font in your script to assign it in the inspector. This way the path is no longer hardcoded and Godot will update it automatically if you move or rename the resource.
So your code would become:

export(DynamicFont) var _dynamic_font

func _draw():
    draw_string(_dynamic_font, Vector2(12, 32), 'aC',

And if you want no code at all, you can use the Label node.

by (29,360 points)
selected by
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.