Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I'm trying to get the parent node of a child node.
- Example
- parent
- child
- child
- child
- I want to get the parent. A script is attached to the last child in the hierarchy. I was using getparent().getparent().get_parent(). what would be the best way to goo about it.
- nodes gdscript hierarchy variable
- asked 6 hours ago in Engine by vonflyhighace2 (78 points)
- 3 Answers
- 0 votes
- I would use get_tree().get_root().get_node("parent"). There may be other ways though.
- answered 6 hours ago by Non0w (18 points)
- +1 vote
- Documentation is your friend. You can use absolute path: get_node(“/root/parent”)
- answered 4 hours ago by GlaDOSik (238 points)
- 0 votes
- also , you can use get_owner() method... but the previous answer are better ... I usually use this method to access immediate parent of a node ....
- ------------------------------------------------------------------------------------------
- You can use ".." to traverse through the ancestors.
- If you have a hierarchy like this
- world
- boat
- gun
- If you call get_node("../../") from the gun, you will get the world node.
- Additionally, you can do in script
- export(NodePath) var parent_path
- and then you can browse and choose required node from the editor itself. Later if you change the hierarchy, you just need to select the path in the editor and don't require to edit the script.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement