Topic was automatically imported from the old Question2Answer platform.
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: foo(@"grp_Player:position:x") OUTPUT: 40
UPDATE:
please don’t answer as get_node(@"grp_Player").position.x for I need the get the value only from the nodepath itself.
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]]
print(obj)
oooooo I didn’t I could access my object’s properties like an array!
Thank you so much!
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)