When scripting in Godot, there are many components that make up a single game object. For a player object, you may have a sprite component, the kinematic body, and the collision shape.

Do you know how annoying it would be if you had to write a script for each node, instead of just having the parent node handle all of them? That’s a lot of work for no real benefit.

Thankfully, Godot has a feature for that.

Dollar Sign Operator

In Godot, there is an operator known as the dollar sign operator. This allows you to reference children of the parent node within a script. By using the operator as a shorthand, you can easily access nodes by their name in the UI. Here’s an example:

func die()->bool:
# Accessing the node NAMED CollisionShape2D in the UI.
# If There were subchildren of that node, we'd access it like so $CollisionShape2D/ChildNode
$CollisionShape2D.set_deferred("disabled", true)
return true
# Alternate Syntax
func die_type_2()->bool:
# Accessing the node NAMED CollisionShape2D in the UI.
# This syntax is useful if you want to chain gets with the dot operator; dollar sign does not work with the operator.
get_node("CollisionShape2D").set_deferred("disabled", true)
return true

As you can see, we use the dollar sign operator to access the node named CollisionShape2D. This is an important distinction to make. We are accessing the node by name and not by the type of node that it is. We can also access the node’s subchildren using the slash(/) saving us time when coding.

Note, we also show an alternate syntax.

The alternate syntax is to simply use the get_node function. It’s best used when you want to access a node on a packed scene or a node that only exists at run time. The dollar sign operator does not work with elements at run time when chaining with the dot operator; this is because you can’t chain a . with the $ operator when accessing nodes. In my opinion, the best use of get_node is when you want to access nodes on an instance(packed scene).

With that said, I hope this little tip helps improve your games in Godot! Keep making your awesome games!

Additional Readings
One Shot Rpg Dollar Sign vs Get Node

%d bloggers like this: