How do I get a value from a NodePath

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

how do I get a value from a node path?
for example: @grp_Player:position:x
grp_Player is a KinematicBody2d by the way.

is there a function for this?
for example if my grp_Player’s position is equal toVector(40,32),
I need a function that does something like this:

please don’t answer as get_node(@"grp_Player").position.x for I need the get the value only from the nodepath itself.

:bust_in_silhouette: Reply From: jluini

You could split the path using the colon as separator and look up through the objects properties.

var path = "grp_Player:position:x"

var properties = path.split(":")

var obj = get_node(properties[0])
for i in range(1, len(properties)):
	obj = obj[properties[i]]


oooooo I didn’t I could access my object’s properties like an array!
Thank you so much!

Mike Trevor | 2020-04-05 08:43

You could also use Object#get_indexed for the properties.

var properties = path.split(":", true, 1)
var obj = get_node(properties[0])
if properties[1]:
  obj = obj.get_indexed(properties[1])

hanke | 2021-07-20 08:14

:bust_in_silhouette: Reply From: bobojo

There is get_indexed ( NodePath property ) where you can feed it a node path like position:x

So a working piece of code should be:

get_node(@"grp_Player").get_indexed ("position:x" )

Now to take in a single node path and separate its properties we use: get_as_property_path ( )

so you final code can look like:

var yourNodePath = "@grp_Player:position:x";
var yourPropertyPath = get_as_property_path ( yourNodePath)
var value = get_node(yourNodePath).get_indexed(yourPropertyPath)

Here are some Links