How to draw strings?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By C:\Flavius


I have an array of points whose coordinates are the segments of a circumference (aka, points like in a clock)

I want to draw numbers (again, like in a clock) in those points. But they are not being drawn, so I changed the code to just:

var font = global.font;

draw_string(font, Vector2(320, 240), "Hug you", Color(1.0, 1.0, 0.0, 1.0)); 

So global.font is just the font of a label, that font I have stored it in a script called global

So, draw a string witha default font, position (320, 240), a string, a color with an alpha and done

Also sorry for the cursewords (censored it to “Hug you”), this thing I just wanted to do it to get a screenshot and use it in a monography I’m writing. I just #ed all the rest of the code and wrote those lines, but as it didn’t work I just got progresively angrier cause I’ve been trying to draw just three bloody characters for idk, a too long time for such a simple task. So, sorry for being angry. I am angry with myself, so that curseword is written for me.

Also all this code is in the _draw function of a canvasitem node.

I once managed to get those numbers drawn but they were behind the circle, so I just moved those two lines, and Bam!, no characters being drawn anymore.

I am sure I am making a very silly mistake, and I just need to add a litle something, but I just don’t know what I have to change.

Okay then, so what should my code look like in order for it to draw “I love you” in the position (320, 240)?

Thank you in advance!

Edit: In terminal “ERROR CanvasItem::draw_string: condition ’ p_font.is_null<> ’ is true.
At scene\2d\canvas_item.cpp:789”

I get the problem has to do with the font, but I don’t know the exact meaning of that error.

How can I set a default font? I tried getting the font directly from the label using get_node(“path”).get_font but I couldn’t find the path to that label.
I tried using first the name of the label, then “res://Content/…path of the scene/main.tscn/name of the label” but it didn’t work so that’s why I tried storing the font in the global script, using global.font = self.get_font inside the label. Idk what else to do. I’ll keep trying.

:bust_in_silhouette: Reply From: Bartosz

You could use Labels to “print” those number e.g

extends Control

export(PackedScene) var Text # this is label with our desired font applyied
export(float) var radius = 20

func _ready():
	var position = Vector2(-radius,0)
	for i in range(1,13):
		var label = Text.instance()
		label.text = var2str(i)
		var angle_in_radians = -(i - 1) / 12.0 * 2 * PI
		var rotated_position = position.rotated(angle_in_radians)
		label.margin_top = rotated_position.x
		label.margin_left = rotated_position.y

you can check full scene at godot-recipes
It basically creates new Label at assigns their position along circumference and Text is just scene that inherits from Label and sets a custom font

:bust_in_silhouette: Reply From: imekon

You need to create a font object like this:

var font

func _ready():
    font =
    font.font_data = load("res://whatever.ttf")
    font.size = 20

func _draw():
    draw_string(font, Vector(0, 0), 'this is some text')

You need to make sure ‘whatever.ttf’ is in your top level directory or change it accordingly.