Writing and reading from a singleton in _Ready

Godot Version

4.5.1

Question

Hiya! I have a gamestate singleton in which I’ve put a gridmap reference. The idea is that levels will set this reference on _ready to the current level’s gridmap, so it can be reliably read from the same source. I have my player script also trying to read this reference on _ready, however it throws a null reference.

My current understanding is that _ready is called on parents first, so the level _ready would trigger before the player’s, however that’s either not the case, or there’s some sort of race condition with the singleton where the variable is being read before the write.

I’ve since implemented a signal in the GameState that is emitted on a setter that remedies this problem, but am still interested in learning why the original implementation failed.

No, it’s the other way around. Parent’s _ready() is called after all of children’s readies have been executed.

1 Like

Ah gotcha, thanks! In that case the signal is probably the way to do it then.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.