Why can't one node access it's sibling in the same scene?

Godot Version



I’m new to Godot, and I’ve been really struggling to get nodes to communicate with each other. I’ve been working on the same problem for a couple weeks now: I have two “scenes” in the world/main/root (whatever you call it) scene. I want to give my enemy AI node a reference to a target node somewhere else in the scene, so that the enemy AI can move towards the location of that target node. But nothing is working: all the documentation says I should be using ‘get_node(“node_path”)’ for this. Yet when I tried this, the enemy script thinks the target node doesn’t exist, EVEN THOUGH THE TARGET NODE IS WORKING AS EXPECTED. I can see the target node firing off signals and moving around on the screen as expected, but the enemy script just doesn’t think it exists. I attached a bunch of screenshots so y’all can see my setup – very frustrated by this. I’ve included only the relevant portion of code, since the script is rather lengthy and mostly not relevant. I can provide the entire script if you like.

Folder structure

You are trying to use a filepath used for loading files with the get_node() function that expects a path to a node in the SceneTree, not to a file in the filesystem. So that setup can not work.

With that path you would load your “target_location.tscn” packed scene using a ResourceLoader or the load() function. After loading you would create an instance. That instance you would add to the SceneTree. Now that SceneTree path to this instance is what the get_node() function would expect.

In the last image if the “Player” node needs the “Monster” node you would need to use get_node(“…/Monster”) in the player script. The “…” to move up one child level to the “World” node and the “/Monster” to get the child node named Monster. Same you would do with the “TargetLocation”.

So, I want the Monster to move towards the TargetLocation. I rearranged the scene and retyped the code, and I’m still getting the same print out.


Please see the documentation on Nodes and scene instance, it has all the info that you need about get_node() or how to ref nodes: Nodes and scene instances — Godot Engine (latest) documentation in English

I think your path has one “.” to much. Be also aware that by the time a node is ready / onready other nodes might not. Child nodes are usually not a problem because they are ready before the parent but siblings or some far off node path might not be fully ready or even exist at the point your script calls ready. See documentation about SceneTree order here Using SceneTree — Godot Engine (latest) documentation in English
The best way to solve this is to wait a frame or use call_deferred() with a setup function as that makes sure that the SceneTree had time to finish the entire node setup.

Does not apply to your current setup but if nodes are all part of the same packed scene you can also use Scene Unique Nodes if you know in advance where everything is, this way you can use a shorter path to ref the node, see Scene Unique Nodes — Godot Engine (latest) documentation in English