0 votes

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
            root.add_child(self)
Godot version 3.4.2
in Engine by (41 points)

1 Answer

+1 vote
Best answer

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.

by (1,466 points)
selected by
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 Frequently asked questions and 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 [email protected] with your username.