What's the difference between get_node("Sprite") and $Sprite?

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

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

:bust_in_silhouette: Reply From: DodoIta

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.

How is Godot3 going towards $ sugar?

hilfazer | 2018-08-24 15:39

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.

DodoIta | 2018-08-24 19:28

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

ZacB | 2018-08-24 19:38

:bust_in_silhouette: Reply From: lincolnpepper

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.