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)
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.
If it is printing 3 times from the _ready() function then this label is being created 3 times.
I don’t see any code that might be doing this so do you create it somewhere else?
Maybe in an autoload?
This sounds like you are making incorrect assumptions about global variables, like you placed a label node with the calculation script that also happens to be a global script.
But global script nodes are created when the game starts, they will not be referring to existing nodes.
And does LineEdit have the scene/line_edit.gd script? Does the Label have the scenes/Label.gd script? If so that means you have an extra global copy of each node that you are operating on, but is likely hidden behind your background node. Your scripts already have node paths to the label through $"../Label" why not use that instead?
If you run the game and check the “remote” tab in the scene tree it will show you the real in-game scene tree. This can help with such node/global issues too