Is there a way to get a scene tree from an orphaned node?

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

Let’s say I have an orphaned node, and I want to give it parents. Is there a way to do that from inside the orphaned node?

func foo():
    if !is_inside_tree():
            var root = get_tree().get_root() #get_tree returns null

is this ?

ramazan | 2022-06-14 14:35

:bust_in_silhouette: Reply From: AlexTheRegent

You can get access to SceneTree from singleton if you have any. Just use get_tree method on singleton class. So if you have autoload for Provider node, you can call Provider.get_tree().get_root() inside your orphan to get access to root.