how to specify the path to the node from the object script, which is created as a child via add_child

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

There is a player object on the launched scene, another scene is loaded to this scene via load and add_child, but how can I specify the path of an existing node in the script of the added scene?
how to refer to a node that is in another scene?

for example, when you need to point to the player node, for the added object when implementing the navigation mechanics

:bust_in_silhouette: Reply From: crossbito

It depends on your preferences. One approach is to create a signal in the root scene to facilitate communication between child nodes. When a child node receives the signal, it can choose to perform a specific action based on the originating node. Make sure to connect the signal in the ready function if the node need to get the notification, or activate the signal if you need the node to trigger the notification.

Additionally, you can use groups to obtain nodes using the get_tree().get_nodes_in_group(“player”)[0] function for example to get the player.

Another option is to utilize globals or resources to communicate between nodes. These provide a shared space where variables or data can be accessed and modified by different nodes.

global variables in a singleton seemed to me something more suitable, thanks. But I have an additional question, can I add obstacle2d to characterbody2d in addition to navigationagent2d? Around this point, I stopped, trying to implement more or less normal navigation of opponents.

lakido | 2023-06-06 17:18

:bust_in_silhouette: Reply From: ShatteredReality

Generally, you don’t want to use node paths to go up a node. You should use signals instead. However, if you really want to, use get_node(“…”) or $“…” to go up a node. For example, if your node layout is

  • Base
  • Player
  • Scene

From “Scene” to “Player” the node path would be “…/Player”. Or you could use the absolute path from the root. “/root/Base/Player”.

The scene is added by the add_child method in the Base Script. But for its navigation, need the variables of the Player node (path). Although I haven’t quite figured out how to properly set up this navigation and in general with signals, for example velocity_computed and etc.

lakido | 2023-06-06 17:55