+1 vote

Hi everyone,

is there a difference between calling get_node with the "@" prefix or using the "$"?
Is there a difference in terms of performance?

The documentation (https://docs.godotengine.org/de/stable/classes/class_nodepath.html) says, that a NodePath will be parsed ahead of time. So, I wonder, if this is the case with the "$" sign, too?

Regards

abgenullt

in Engine by (19 points)

1 Answer

0 votes

The general convention is to use "$" to assign a variable with the path to be used throughout your code, rather than having to call get_node() various times for the same thing. This leads to cleaner code that is much more readable.

Using "$" such as

onready var player = $Player

will run when the script has been checked through and will remain usable for the time the script is active in your game. However, by calling get_node(), this acquisition of the node will have to run each time the corresponding function it lies within is called, ie., more than once at the beginning of execution.

by (138 points)

Both syntax can be used to store a node in a variable and both can be used to fetch a node every time it's needed.

There's no difference, $ is a syntactic sugar for get_node(@).

In practice, there isn't a whole lot of difference for most projects (according to KidsCanCode).

$ is a lot better for readability

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 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 webmaster@godotengine.org with your username.