Getting references to nodes in different branches in the same scene

Godot Version

Godot 4.4

Question

Hi, I have been facing this issue on my current project and I would like some help. Although the “call down signal up” principle is good advice sometimes, due to project complexity or poor structuring in previous code that can’t be changed without a huge refactor, I need to get a reference in a weird path for example get_node(“…/…/ex1/ex2”). The way I’ve been tackling this issue is by having a LevelContext singleton that stores references to important nodes on the current scene. However, this raises an issue, when one entity needs one of these references during it’s _ready call (ex: an enemy that needs the reference to the player to track him upon spawning) the singleton may or may not have the reference available because the player needs to be ready first to give it’s reference to the singleton. This method is ugly and very dependent on the ready order of the scene which really isn’t a good thing. I would like to know other ways people have tackled this issue. Thanks for the help.

A simple way that might help is using scene unique nodes. It’s really simple. The documentation also mentions using groups or find_child(). You could also store the node by variable at one point and access it that way. I think Singletons are a great way to do that, though.