Child exists and not "_ready" before parent is "_ready"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hazer_hazer

Hi, everyone.
I have a node Road which has some Spatial nodes.
The main scene Game has Road as a child. Game calls $Road.advance() method in _ready. As a result I have an error Attempt to call function 'advance in base 'null instance' on a null instance..
It, as I think, means that Road is not “ready” or it is an engine bug (0_o).
I’ve putted print(get_children()) in Game._ready method and had [].
I always thought that method _ready in the parent is called only after all children called their _ready. This is really surprising for me.
Have anyone stucked with a problem like this one? It might be something common.

I tried to put print("Road is ready") to Road._ready and it wasn’t ever printed.

I cannot add images now, sorry, but the hierarchy is really simple – just a child node in parent, and it is never “ready” ;(

Game.gd

func _ready():
    $Road.advance() # ERROR: Road is nil

Road.gd

func _ready():
    print(”Road is ready") # Never printed 

func advance():
    # Some logic

I’m absolutely sure, that, at least in the editor, the Road is a child of the Game. But, maybe, something out of user view is wrong, maybe inside .tscn files. Anyway, I’ve tried recreating Road and Game scenes from scratch – does not help. Now have no idea why this may happen.

Have you tried using Play Scene (F6 by default)? Does the problem repeat?

hilfazer | 2022-04-10 07:52

Ohhh… The problem is that Game is present in AutoLoad, i.e. it is a singleton.
Thanks for the idea about scenes.

hazer_hazer | 2022-04-10 12:00

:bust_in_silhouette: Reply From: hazer_hazer

The problem was in that Game is singleton (added to AutoLoad), thus, as script, it was ready, but not as a scene.

:bust_in_silhouette: Reply From: angstyloop

Check out https://forum.godotengine.org/57793/parent-fuction-yield-wait-until-child-is-_ready
for solutions to similar issues, using yield or signals on the child node

: )

Thanks! Post any questions!