How i can change nodes of object from autoloading script?

Godot Version

4.22

Question

I create a test scene that include background ColorRect, Timer and Santa node(this include some nodes and UI node, that have a Label node named “Best_int_label”. And i create an autoloading script “data” that have a code:

extends Node

var best_score = 0
var file

func _ready():
if FileAccess.file_exists(“data.txt”):
file = FileAccess.open(“data.txt”, FileAccess.READ)
best_score = int(file.get_as_text())
file.close()
new_best(best_score)

func _process(delta):
pass

func new_score(score):
if score > best_score:
new_best(score)

func new_best(score):
best_score = score
file = FileAccess.open(“data.txt”, FileAccess.WRITE)
file.store_line(str(best_score))
file.close()
#HERE I NEED TO CHANGE TEXT OF “test/santa/ui/best_int_label”

How i can change text of “test/santa/ui/best_int_label” at last line of this script? I cant do this with using “get_node(“test/santa/ui/best_int_label”)”, because godot give an error that node not found.

I want to make sure that when game opening, it reads data from “data.txt”. And when player set new best score, game save it to data.txt. Maybe i can do this in different way?

Thanks



Manually Hardcode the node path is not the best practice, because the tree may change and your path will not automatically update it
instead try use Global Event Bus Signalling
emit the signal and connect the signal at UI script. it should do the work

example how to do it:
in your Santa Script:

func _ready():
	EventBus.update_ui.connect(func(_score):print("replace this print to the function that update your UI: ",_score))

in your “data”.gd when you want to try to update the score with the signal:

EvenBus.update_ui.emit(score)

in the EventBus script AutoLoad:

extends Node

signal update_ui(new_score)
1 Like

Big thanks bro

1 Like