I am developing a prototype for a game and it seems that the _ready function in objects in scene tree are not executing consistently the first time it plays from when I use 'get_tree().reload_current_scene()' to restart the game.

From placing breakpoints in code, I have came to the conclusion that the reason why it might not be executing is because 'yield(get_tree().root, "ready")' is blocking the code from executing for some reason.

It seems that getting rid of this, gets rid of certain errors. But due to dependencies of executing the the parent's ready functions first, it may not work.

How can I fix this type of error and make my code less error prone when using this yield function?

Best regards

Basically never use yield. It causes too many problems for no gain.

I don't think yield will work for a scene reload. I use it and it works for a lot of wait type things where you want to wait so many frames or for a signal or something. A scene reload causes a loss of variable values if it's the main scene so you can't expect to use it there. Normally, if you want to keep information, you can use an autoload script and save the variables there and then reload them from the ready function when the scene reloads, or just always use the autoload variables.

@fire7side said: I don't think yield will work for a scene reload. I use it and it works for a lot of wait type things where you want to wait so many frames or for a signal or something. A scene reload causes a loss of variable values if it's the main scene so you can't expect to use it there. Normally, if you want to keep information, you can use an autoload script and save the variables there and then reload them from the ready function when the scene reloads, or just always use the autoload variables.

I was initializing the object (not passing scene data around). I was using the code 'yield(get_tree().root, "ready")' to make sure the parent nodes get initialized first.

So, as I said, don't use yield (like ever). The cases where it is needed are very advanced and at that point you will understand when you need to use it (for example asynchronous network code). It is best just to avoid it altogether as it creates way too many problems as you have just seen.

To answer your question, ready on children and parents will always be called in the same order. Each child is initialized one by one, and when they are all ready, the parent will become initialized and then ready is called.

For this reason, you should almost never call up in the hierarchy (or to the side) when dealing with ready or initialization code. Meaning do not do anything with the parent, the parent's parent, or any siblings. Try to make each script self contained. If there are links that need to be established, do this on the parent. When the parent is ready, you are guaranteed all children are ready, and you can set their variables freely.

a year later