+13 votes

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.

in Engine by (232 points)

Maybe gettree().currentscene is what you need.

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)

5 Answers

+17 votes
Best answer

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.

by (1,086 points)
selected by

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

+4 votes

Perhaps:

var current_scene = get_tree().get_current_scene()
by (204 points)

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.

0 votes

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

by (143 points)

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

child_node.owner

returns the node I am looking for.

0 votes

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)

by (304 points)
0 votes

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.

by (14 points)

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 getnodesin_group() returns an array with a single element)

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.