Godot Version
v4.2.1.stable.mono.official [b09f793f5]
Question
Hello, I am trying to make a Game script that is a singleton that will take care of a lot of general logic in the game. I am trying to make a couple of Utils scripts, to prevent one long Game.gd script, but my problem is that I can’t emit my signals right after I have instantiated my scripts. Is there anyway to await for my scripts before I emit my signals?
extends Node
var current_date: int
signal day_changed
signal time_changed
var save_load_system: Save_Load_System
var time_system: Time_System
func _ready():
save_load_system = Save_Load_System.new()
save_load_system.load_game()
if current_date == 0:
current_date = 1
day_changed.emit(current_date)
time_system = Time_System.new()
time_system.set_time()
await get_tree().create_timer(1.0).timeout
time_changed.emit(time_system.get_time())
I think you have to separate your code.
the node called Save_Load_System has a method called _ready()
you can await Save_Load_System.ready signal
after this you would execute the rest of the code you have to.
1 Like
Using call deferred on the emit signal should also solve this problem.
It could be a way but Im not really sure if it is going to be enough.
But you could use the ready signal to know if the node loaded the resource you need.
Node Loader
func _ready():
loading_resourse()
print(“finish”)
Node 2
func _ready():
code
code
await Loader.ready
code
code
Im not sure why, but the _ready() function does not get executed on the resources that I create in my Game script. I will see if there is a notification that can help me
Pretty anti climatic. I can just connect to the signal in
func _init()
as its called before even _ready()
Today I learned haha. Thanks for trying to help though :)!
Edit: Not very good solution, because if the script refers to a node, it hasn’t connected yet 
1 Like
Scene tree: The SceneTree contains the active tree of nodes. When a node is added to the scene tree, it receives the NOTIFICATION_ENTER_TREE notification and its _enter_tree callback is triggered. Child nodes are always added after their parent node, i.e. the _enter_tree callback of a parent node will be triggered before its child’s.
Once all nodes have been added in the scene tree, they receive the NOTIFICATION_READY notification and their respective _ready callbacks are triggered. For groups of nodes, the _ready callback is called in reverse order, starting with the children and moving up to the parent nodes.
This means that when adding a node to the scene tree, the following order will be used for the callbacks: _enter_tree of the parent, _enter_tree of the children, _ready of the children and finally _ready of the parent (recursively for the entire scene tree).
https://docs.godotengine.org/en/stable/classes/class_node.html