reference sibling objects in _ready function

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

Hi all, I’m following the tutorial step_by_step/ui_code_a_life_bar and I’m getting confused about this part:

When you open a scene in the game, Godot creates nodes one by one,
following the order in your Scene dock, from top to bottom. GUI and
Player are not part of the same node branch. To make sure they both
exist when we access each other, we have to use the _ready function.
Godot calls _ready right after it loaded all nodes, before the game
starts. It’s the perfect function to set everything up and prepare the
game session.

But they use the function _ready of the GUI node, not those of a common parent. The GUI and players are siblings and GUI node is listed before in the scene dock.
The manual of _ready tells another thing and doesn’t mention siblings.

void _ready() virtual

Called when the node is “ready”, i.e. when both the node and its
children have entered the scene tree. If the node has children, their
_ready() callbacks get triggered first, and the parent node will receive the ready notification afterwards.

I don’t get how they’re sure the Player exists and can be referenced in his sibling GUI’s _ready function?

:bust_in_silhouette: Reply From: njamster

You’re correct! This does not guarantee that the Player-node exist and would result in an error if it really didn’t. And because the GUI-node is above the Player-node in the tree, it does not even ensure that the Player-node has called it’s _ready-function before. (Even though that’s not relevant in this case as the Player-node doesn’t have one anyway) Consider opening an issue on github.

Bug 4011 opened, thanks for the quick answer.

fusillator | 2020-09-13 23:08