Access a sibling node

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

I would like to know how I could access a brother node easily and quickly. Suppose I have a root node called “main” and two other children “node1” and “node2”, and the latter has a variable that I want to access (could be eg “scale”), as I would do to access it, that of node1?

:bust_in_silhouette: Reply From: Archtects

You can either make it a singleton by
Project Settings > Autoload. Then target your script. (If your talking about scripts)
Otherwise You’d Have to get the node.
like so

get_node("root/main/node1")

So I guess $“root/main/node1” would work, too. I was referring to picking up the node2, so I suppose the following function should work perfectly:

get_node("root/main/node2").scale = Vector2(1,1) # something you want to put

or:

$"root/main/node2".scale = Vector2(1,1) # something you want to put

Thanks for the answer.

JulioYagami | 2018-11-21 09:45

:bust_in_silhouette: Reply From: Zylann

To access a sibling node, you just need this:

get_parent().get_node("SiblingName")

Or

$"../SiblingName"

The advantage is, you don’t need to setup a singleton and your node could be anywhere in the tree, it will still be able to access its sibling.
Not sure why singletons or root access were suggested, they seem overkill to me^^

Sorry Its how I’ve always managed to get things to work. The above has never worked for me at all :confused:

Archtects | 2018-11-22 16:18