![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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