Update label text

Godot Version

4.3

I want the label text to update dynamically based on the calculations generated. The scripts execute correctly and the results are printed in the output as expected, but the label text does not update. “1+1” is the default text.

extends LineEdit

var old_text := ""
signal number_submitted(number)
@onready var line_edit: LineEdit = $"."
@onready var label: Label = $"../Label"

func _ready() -> void:
	line_edit.text_changed.connect(_something)
	connect("text_submitted", Callable(self, "_on_text_submitted"))

func _something(text: String) -> void:
	if text.is_empty() or text.is_valid_int() or (text == "-" and old_text.is_empty()): 
		old_text = text
	else:
		line_edit.text = old_text

func _on_text_submitted(submitted_text):
	emit_signal("number_submitted", submitted_text)
	check_answer(submitted_text)
	clear()
	print(submitted_text)

func check_answer(submitted_text: String) -> void:
	if submitted_text == Calculation.converted:
		print("Correct! Generating new calculation...")
		Calculation._generate_calculation()  # Calculation is a global variable defined in the Label, and converted is accessed from it
	else:
		print("Wrong, try again.")

extends Label

var rng = RandomNumberGenerator.new()
var num1 = 0
var num2 = 0
var cal = 0
var converted = ""

func _generate_calculation() -> void:
	num1 = rng.randi_range(1, 10)
	num2 = rng.randi_range(1, 10)
	cal = num1 + num2
	self.text = "%d + %d" % [num1, num2]
	converted = str(cal)
	print(num1, "+", num2)

(post deleted by author)

(post deleted by author)

I am not sure why its not working but can you try this?

text = str(num1) + " + " + str(num2)

This still doesn’t work. I found a solution to update the label text by placing the function inside func _ready(). However, in the output, it prints three calculations, but the label text doesn’t update according to the generated calculations. Instead, it only changes at the beginning.

1 Like

Do you checked the debugger?