+1 vote

I'm wondering what's the difference between get_node("Sprite") and $Sprite and which is better to use.

in Engine by (27 points)
retagged by

2 Answers

0 votes
Best answer

They are the exact same. $ is simply a shortcut for get_node. However, you can't do multiple nodes in a row with $. For example, you could do get_node("Area2D").get_node("Sprite") but you can't do $Area2D.$Sprite or $Area2D$Sprite. In both methods you can use a path within the quotations though, like this: $"Area2D/Sprite".

They both function the same, but $ is more compact and i use it more often, but if i have a lot of them then i use get_node so i can better understand what's going on.

by (241 points)
selected by
+1 vote

I don't think there's much difference between the two approaches, obviously the second one has more syntactic sugar and helps writing more compact lines.
It's probably best to get used to the second one, since it looks like Godot 3 is going towards that direction.

by (515 points)

How is Godot3 going towards $ sugar?

I'm just guessing it, since it's a new addition and it helps writing less code.
Furthermore new tutorials use it, so I'm guessing they're kinda trying to push towards that approach.
There's no correct or wrong way to do it in any case, if one likes get_node he's free to use it.

I really often use $. But sometimes I use get_node() when I have a variable node path.

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.