So recently I’ve been doing the user interface for Trivo, and I had been referencing all the control nodes that I needed by creating string variables and storing the path to them.

This worked, but anytime the scene’s node hierarchy changed, my hard-coded string paths would all be incorrect and have to be updated. This caused changes to the UI to be quite cumbersome since I had to copy new node paths over to my code each time.

I got fed up with the workflow and looked for another way, and I can’t believe it took me this long to figure a cleaner way to manage references to the nodes I want to manipulate in my scripts.

Essentially, Godot has a nice feature that allows you, in the editor, to set a scene’s variable. Godot also features a handy variable type called a NodePath which is just essentially reference to a node in a scene. Combining these two features allows one to easily add NodePaths to your script which you can then set to nodes in your scene that will automatically update even if they’ve been moved.

A picture of the script variables, which you can set to nodes in the scene
The node path selection pop-up in Godot

This is pretty simple, but for whatever reason I never leveraged it until now. Glad I’m using it now though, makes changes to the scene tree much easier when I don’t break all the references in the script.