Should I use variables for child nodes

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

I have a short question. Should I use local variables for referencing child nodes? Will it be better then using $ChildNode syntax?

:bust_in_silhouette: Reply From: ababen

It is better to use a variable because if the node path will change as you continue working on your project you’d have to go through every $ChildNode and change it to the new path. If you use a var, you’d just have to change the path in the var declaration.

But the best way to do it is to right click on the child and press “access as scene unique name”. that way you can access the child by its name, and it will persist even if you move it up or down the tree.
Here’s an example of using a scene unique name to reference a sprite:

var child = $'%Sprite2D'