+9 votes

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.

in Engine by (259 points)

7 Answers

+5 votes

I would use get_tree().get_root().get_node("parent"). There may be other ways though.

by (33 points)

You solved a huge issue for me,
Thank you so much!

+6 votes

Documentation is your friend. You can use absolute path: get_node(“/root/parent”)

by (814 points)

Is it possible to do this if the parent is not based on an absolute path. I have many different parent nodes so i can't use a fixed node path to the parent.

If the node "parent" is not the first node in the tree (it's not connected to the root viewport) and your example is general, absolute path won't work. If you have many parent nodes connected to the root viewport, it should work.

But if you know how many levels up the parent is you can also use a relative path: get_node("../../../parent")

+13 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 ....

by (114 points)

wait, if you use the get_owner() you can get the parent of the node you are using this in??
This is actually pretty usefull

parent
  child
      child
         child

In this situation you will get the parent! Thanks for this answer! :D

+15 votes

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.

by (750 points)

Does it matter how many nodes are in the hierarchy? If there are a bunch of nodes in the parent will this still work? I have to get the parent during runtime so a fixed path wouldn't work for my case.

It should work.

+5 votes

Assuming this tree:

root (default viewport)
 Game
  Level1
   Player
    Gun

Script in Gun:

var game = get_owner()
var player = get_parent()
var root = get_tree().get_root()
by (211 points)
+1 vote

The previous answers also look good.
But depending on your implementation, you might as well do it that way:

  • In the parent scene:
add_to_group("current_level")
  • In the child scene:
var parent = get_tree().get_nodes_in_group("current_level")[0]
by (108 points)
0 votes

Traverse SceneTree up in Godot is expensive. And Best practices recommends passing reference from parent to child. So this is "true way"

var scene_tree:SceneTree


func _ready() -> void:
    scene_tree = get_tree()
    scene_tree.connect("node_added", self,"on_node_added")
    for i in get_child_count():
        var child:Node = get_child(i)
        if child.is_in_group("wants_parent_ref"):
            child.my_parent = self


func on_node_added(node:Node) -> void:
    if !is_a_parent_of(node):
        return
    if node.is_in_group("wants_parent_ref"):
        node.my_parent = self
by (846 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.