How to display an int using a label

Godot Version

4.6

Question

In my characyter’s scene I added a canvas layer with a Label inside, but I cannot make it appear.
I have added
@export var progression: int = 0
@onready var label: Label = $Label
func _on_progression_value_ready() → void:
$Label.text = str(progression), but it still does not want to work

$Label is not the correct path to your label node.

What is a correct path?

Just follow the node names from the node that has the script to the target node and separate them using /. Exactly like specifying file paths in computer’s file system.

If the script is in scene’s top node you can right click on the target node and do “copy node path” and paste it into the script. Or you can just drag the node from the scene tree into the script.

But why? It’s all in the same scene, so why would I need to add a node path?

How else would the system know that you want to access that specific node?

I still don’t understand how to add it. Could you help?

Drag the node into the script

Do not write “@onready var …” lines by hand. Press Ctrl and drag and drop the node from Scene tree to your code. That’s how you always get the correct path.

Oh okay, now I have done it and it’s not crashing, but it’s not displaying anything

Post the new code.

extends CharacterBody2D
@export var progression: int = 0
@onready var label: Label = $“CanvasLayer/Progression Value”
func _on_progression_value_ready() → void:
[TAB]$“CanvasLayer/Progression Value”.text = str(progression)

You are already getting the label node to the label variable on third line.You do not need to get it again in the fuction. Use the label variable instead:

func _on_progression_value_ready() -> void:
	label.text = str(progression)

E 0:00:01:303 _on_progression_value_ready: Invalid assignment of property or key ‘text’ with value of type ‘String’ on a base object of type ‘Nil’.
char_controller.gd:74 @ _on_progression_value_ready()
char_controller.gd:74 @ _on_progression_value_ready()

What say you @Dizzy_Caterpillar? How do we solve this?

I don’t know for sure why using label variable doesn’t work. My guess is that the function _on_progression_value_ready is called before @onreadyvariables are set. You could add _ready function and check which function is called first.

func _ready() -> void:
	prints("_ready")

func _on_progression_value_ready() -> void:
	prints("_on_progression_value_ready")
	label.text = str(progression)

If “_on_progression_value_ready” is printed first, label variable is still null in the _on_progression_value_ready function. How to fix this, I don’t know without seeing more code.

The function certainly gets called because the error message says the errors happened inside that function. But label’s ready will happen before character body’s ready. And label is initialized in character body’s @onready.

extends CharacterBody2D

@export var speed = 350
@export var progression: int = 0
@onready var label: Label = $“CanvasLayer/Progression Value”
func get_input():
var input_direction = Input.get_vector(“left”, “right”, “up”, “down”)
velocity = input_direction * speed
#nput_direction)

func _physics_process(delta):
get_input()
move_and_slide()

####KEYS####
func _process(_delta):
if Input.is_action_just_pressed(“exit_program”):
get_tree().quit()

if Input.is_action_just_pressed("interact"):
	print("Interacti")
	#interact()
	
if Input.is_action_just_pressed("cancel"):
	print("cancelling")
	#cancel()
if Input.is_action_just_pressed("attack"):
	print("attack")

func _ready():
#Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN) #Hide cursor

# GAME SAVE


####RND FUNCTION####

	
var file_path = "user://rnd_info.md"
var file = FileAccess.open(file_path, FileAccess.WRITE_READ)

if file:
	var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	var rng = RandomNumberGenerator.new()
	rng.randomize()

	var length = 16  
	var random_text = ""

	for i in range(length):									#
		var index = rng.randi_range(0, chars.length() - 1)  	
		random_text += chars[index]						    #

	file.store_line("FlEcoDr_DQ %s" % random_text)
	file.close()

	print("File created at:", file_path)
else:
	print("Failed to create file")

func _on_progression_value_ready() → void:
label.text = str(progression)

Here’s everything I have in the script

Either put label.text = str(progression) inside character body’s _ready() or use the literal path in _on_progression_value_ready() like you did before. The former is the better solution.

Still doesn’t work. I’ll just remove this I don’t need it that bad