Advertisement
otorp2

get parent node from child

Jun 25th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. I'm trying to get the parent node of a child node.
  2. Example
  3.  
  4. parent
  5. child
  6. child
  7. child
  8.  
  9. 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.
  10.  
  11. nodes gdscript hierarchy variable
  12.  
  13. asked 6 hours ago in Engine by vonflyhighace2 (78 points)
  14. 3 Answers
  15. 0 votes
  16.  
  17. I would use get_tree().get_root().get_node("parent"). There may be other ways though.
  18. answered 6 hours ago by Non0w (18 points)
  19. +1 vote
  20.  
  21. Documentation is your friend. You can use absolute path: get_node(“/root/parent”)
  22. answered 4 hours ago by GlaDOSik (238 points)
  23. 0 votes
  24.  
  25. 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 ....
  26.  
  27. ------------------------------------------------------------------------------------------
  28. You can use ".." to traverse through the ancestors.
  29. If you have a hierarchy like this
  30.  
  31. world
  32. boat
  33. gun
  34. If you call get_node("../../") from the gun, you will get the world node.
  35.  
  36. Additionally, you can do in script
  37. export(NodePath) var parent_path
  38.  
  39. 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