how to get the root parent from a child node.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By vonflyhighace2
:warning: Old Version Published before Godot 3 was released.

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 get_parent().get_parent().get_parent(). what would be the best way to goo about it.

:bust_in_silhouette: Reply From: Non0w

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

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

chunkyPug | 2021-02-12 21:43

:bust_in_silhouette: Reply From: GlaDOSik

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

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.

vonflyhighace2 | 2016-06-27 14:10

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.

GlaDOSik | 2016-06-29 20:31

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

jarlowrey | 2017-05-03 20:24

:bust_in_silhouette: Reply From: RezaPouya

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 …

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

FashionKiler | 2018-05-23 10:17

parent
  child
      child
         child

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

coderpy | 2019-08-11 01:09

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

AnntAgonizer | 2023-01-22 21:49

It is, it’s just the getter method of Node’s “owner” property

Sharkalien | 2023-02-10 17:40

:bust_in_silhouette: Reply From: vinod

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.

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.

vonflyhighace2 | 2016-06-27 16:27

It should work.

vinod | 2016-06-28 00:44

:bust_in_silhouette: Reply From: luislodosm

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()
:bust_in_silhouette: Reply From: kurtsev0103

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]
:bust_in_silhouette: Reply From: yrtv

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
:bust_in_silhouette: Reply From: marilynplummer343

DECLARE @id INT = 6
;WITH parent AS
(
SELECT id, parentId, 1 AS [level] from tbl WHERE id = @id
UNION ALL
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