Skip to content

Instantly share code, notes, and snippets.

@regakakobigman
Last active March 29, 2024 01:31
Show Gist options
  • Save regakakobigman/5a3888ac384bd11378a99ba2dd2e2fcf to your computer and use it in GitHub Desktop.
Save regakakobigman/5a3888ac384bd11378a99ba2dd2e2fcf to your computer and use it in GitHub Desktop.
Gets the property of a node using a NodePath
# This is an awful solution, but NodePath is missing an important method, so there's no great alternative that I've found.
# Try me with get_node_property(self, "Control/Spatial/CollisionShape2D:shape:extents:x")
func get_node_property(from: Node, path: NodePath):
assert ":" in path as String # Causes a hard crash
path = path as NodePath
var node_path = get_as_node_path(path)
var property_path = (path.get_concatenated_subnames() as NodePath).get_as_property_path()
return from.get_node(node_path).get_indexed(property_path)
func get_as_node_path(path: NodePath) -> NodePath:
assert ":" in path as String
path = path as NodePath
var node_path = path as String
var property_path = path.get_concatenated_subnames() as String
node_path.erase((path as String).length() - property_path.length() - 1, property_path.length() + 1)
return node_path as NodePath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment