Godot Engine – Creating references to nodes

Oftentimes you’ll be using $ or get_node() to get a node path in your scene tree. This works great – until you rename or move the node and everything breaks.

But there’s a way to make your life easier! You can save a reference to your node that doesn’t break, even if you rename or move the node around.

Let’s say you have KinematicBody node named Player with a Camera node on it. You’d like to store a reference to the camera to access it later.

Add a script to your Player node and add this line:

export (NodePath) var my_camera

Now select the Player node and take a look in the inspector. You’ll see a new Property called My Camera:

Click on the Assign… button and select your Camera node. You can now access the camera inside your script by typing get_node(my_camera), even if the node is renamed or moved around later.

Great! No more tedious fixing of broken node paths.

Visit the Godot Tutorial main page for more Godot tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top