Can anyone explain how can I use Nodepath to store reference to a property in a variable ?

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

let’s say I have two big nodes, far away in scene tree hierarchy.
One node has property var controller : Node
this property is being set frequently, so its value is being various references to a various nodes.
I would like the other big node, to have a variable, that is a reference to controller property itself, not to its current value. So when first big node sets this property, the other one also has that information updated.

:bust_in_silhouette: Reply From: jgodfrey

There’s a number of ways you could handle this. One would be to create a signal that fires each time the value gets updates. Then, anything else in your code that’s interested in that update could subscribe to that signal and be notified as the value updates.

A simpler solution might be to store this reference in a single, global (AutoLoad / Singleton) variable. Then anything in your game could either update or access the value as needed. While that’s not necessarily a great design, it’s simple and will solve the problem.

If that sounds feasible to you but you need additional details to set it up, post back.

I actually wished for even simpler sollution :slight_smile:
Arrays, dictionaries and Nodes are passed via reference in Godot, as opposed to most of the other data types, which are passed via value.
Two different nodes may have different variables leading to the same array or dictionary, and when one node does changes to it, the second one always has this value updated.
I am sure there must be a way to pass other data types this way, I imagine this would be very simple and ellegant sollution for many problems. I believe NodePath type is created for this reason, but I can’t understand its constructors and methods.

Inces | 2022-12-17 16:07

NodePath isn’t what you are looking for.

A pre-parsed relative or absolute path in a scene tree, for use with
Node.get_node() and similar functions

That is something like “res://MyRootNode/MyChildNode/MyGrandChild”

The autoload is the best answer.
If a property of NodeA is also a property of NodeB then it does not belong to either of them and you shouldn’t be creating/instancing that property in either node.
That property is a likely candidate for a class but even if it is a simple variable it should be created/instanced outside those nodes, then doled out to the objects that will use it.

LeslieS | 2022-12-17 19:10