How to reference nodes that change path locations?

Godot Version

4.2.2 Stable

Question

Howdy y’all, I’m kind of a noob here. How do I reference a node whose nodepath changes during the course of the script? I’m trying to a make a camera zone in a 3D game where, when the player enters it, the camera will switch to follow a set path at a different angle. I think I can do this by reparenting the player’s Camera3D child to be a child of the PathFollow3D, but now when I do that I can no longer use $Camera3D in the code to refer to the camera. Likewise, in other code outside the player scene, I use the camera’s full nodepath to reference the camera, and when the camera gets re-parented, the nodepath is now outdated and invalid.

Is there a way to reference the camera regardless of it’s path or parent?

If you have the reference before it gets reparented you don’t need to get it again. Just get node once and cache it in a local variable.

You can setup a group with the camera class.

You can provide a global/static API to get the camera.

Unique identifiers.

1 Like

@pennyloafers is right, store the node reference into a variable like var camera: Camera2D, or export to the editor with @export var camera: Camera2D for assigning node in the inspector view.

Thank you! Exporting the nodes and setting them in the editor seems to work the best in this case!