This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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

Godot version 3.5.1
in Engine by (26 points)

1 Answer

+2 votes
Best answer

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'
by (194 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.