How to get root node of a Scene (not of whole tree)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By zdimaria

I know I can use

get_tree().get_root()  

or

get_node('/root') 

to get the root of my entire tree.

How can I get the root node of the scene that my current node is in?

I dont want to

get_parent().get_parent()...etc

etc and I also dont want to

get_node('../../..') 

because I don’t know how deep this node is into the tree.

I’ve been searching but haven’t found a clean way to do this. An unclean way that will work is by placing on my SceneRoot node:

scene_root = self

and then on every node in the scene:

scene_root = get_parent().scene_root

but this is tedious to add to every single node I make.

Maybe get_tree().current_scene is what you need.

davidoc | 2018-03-06 02:56

This seems to run the current scene of the whole game. What I’m after is the root node of the scene file that the currently running script exists in. So if I have a Player with an instanced weapon in his scene, and then down in that weapon tree I run a script, I want the root node of the weapon scene (because the script exists in that .tscn)

zdimaria | 2018-03-06 08:00

var current_scene = get_tree().get_current_scene()

This is not what I am looking for. This returns the current scene that the game is in. I want to know the root node of the .tscn file that the running script is in.

for more you can visit: www.customcabinetsnc.com

customcabinetsnc | 2022-11-27 23:16

My solution is:
Create a singleton autoload script with a variable. My class/script/singleton is Consts.
I have a variable in Consts.

var root
My root scene has simple command

func _ready():
Consts.root = self
I can reach my root node from everywhere as Consts is a singleton, autoloaded script.
Usage: Const.root.add_child(newNode)
like my project User kaylamarquez - Godot Engine - Q&A

kaylamarquez | 2023-02-08 21:30

Then you can just use the variable ‘root’ to access the scene root.

If you are creating any other nodes at runtime, just make sure the node in charge of creating them has root and can pass it on. check out User brendaray - Godot Engine - Q&A

brendaray | 2023-02-08 23:00

Then you can just use the variable ‘root’ to access the scene root.

If you are creating any other nodes at runtime, just make sure the node in charge of creating them has root and can pass it on like my project User joyceconley - Godot Engine - Q&A

joyceconley | 2023-02-08 23:58

Create a singleton autoload script with a variable. My class/script/singleton is Consts.
I have a variable in Consts.

var root
My root scene has simple command

func _ready():
Consts.root = self
I can reach my root node from everywhere as Consts is a singleton, autoloaded script.
Usage: Const.root.add_child(newNode)
like User DiannHays - Godot Engine - Q&A

DiannHays | 2023-02-09 01:38

:bust_in_silhouette: Reply From: dodgyville

Perhaps:

var current_scene = get_tree().get_current_scene()

This is not what I am looking for. This returns the current scene that the game is in. I want to know the root node of the .tscn file that the running script is in. This should not change even when instancing this scene into another scene.

zdimaria | 2018-03-06 07:58

:bust_in_silhouette: Reply From: eska

child_node.owner is the root node of the scene that child_node is saved in.

This doesn’t work for runtime-generated nodes though, since they’re not saved as part of the scene file.

It also does not work for the scene’s root node itself, but you can check whetherroot_node.filename is an empty string or a path to a scene file. If it’s not empty, it’s a scene root node.

Awesome, thank you! Exactly what I was trying to find.

zdimaria | 2018-03-06 17:45

1 Like
:bust_in_silhouette: Reply From: bitwes

Is the “root” that you are looking for some special kind of object? You can use a loop and get_parent and is to check each parent’s type to see if it’s the top kind of object. Or maybe the object that the “root” node is attached to is some special object. You could use the same approach and then just backup one once you find your way out of the tree.

You could also use the scene_root var you describe above as a flag instead of reference. Every node inherits from some object that has var scene_root = false and you set it to true on the root node. Then you just loop, calling get_parent until you find on that has the flag set to true

Thanks, but the answer above was exactly what I was looking for.

child_node.owner

returns the node I am looking for.

zdimaria | 2018-03-12 16:53

:bust_in_silhouette: Reply From: DavidPeterWorks

My solution is:
Create a singleton autoload script with a variable. My class/script/singleton is Consts.
I have a variable in Consts.

var root

My root scene has simple command

func _ready():
	Consts.root = self

I can reach my root node from everywhere as Consts is a singleton, autoloaded script.
Usage: Const.root.add_child(newNode)

:bust_in_silhouette: Reply From: Jattmackson

In the root of the scene:

signal i_am_groot

func ready():
     emit_signal("i_am_groot",self)

Then connect this signal to any child nodes which need it:

var root

func on_parent_i_am_root(new_root):
        root = new_root

Then you can just use the variable ‘root’ to access the scene root.

If you are creating any other nodes at runtime, just make sure the node in charge of creating them has root and can pass it on.

If the scene is only going to be instanced once:

In the editor assign the parent to group “parent”
Then in the child scripts:

var root = get_tree().get_nodes_in_group("parent")[0]

(The [0] is becuase get_nodes_in_group() returns an array with a single element)

Jattmackson | 2021-02-25 10:36