+14 votes

I'm trying to get the parent node of a child node.


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 (274 points)

8 Answers

+6 votes

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

by (35 points)

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

+7 votes

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

by (834 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")

+24 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 (116 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


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

Damn, this is an actual built-in statement? Why isn't this in the docs?

It is, it's just the getter method of Node's "owner" property

+16 votes

You can use ".." to traverse through the ancestors.
If you have a hierarchy like this


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 (751 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.

+7 votes

Assuming this tree:

root (default viewport)

Script in Gun:

var game = get_owner()
var player = get_parent()
var root = get_tree().get_root()
by (328 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:
  • In the child scene:
var parent = get_tree().get_nodes_in_group("current_level")[0]
by (162 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):
    if node.is_in_group("wants_parent_ref"):
        node.my_parent = self
by (889 points)
0 votes

;WITH parent AS
SELECT id, parentId, 1 AS [level] from tbl WHERE id = @id
SELECT t.id, t.parentId, [level] + 1 FROM parent
INNER JOIN tbl t ON t.id = parent.parentid
SELECT TOP 1 id FROM parent ORDER BY [level] DESC

by (14 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 Frequently asked questions and 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 [email protected] with your username.