+5 votes
var scene = preload("res://Scenes/MyScene.tscn")
var node = scene.instance()

# Doesn't work. get_parent() returns a viable object, but the node won't get parented to it. When I check the parent of the node it returns null, and debugger gets overflown with errors of this type: C Error: Condition ' !is_inside_tree() ' is true. returned: get_transform()

# But this works fine

I just can't get what's wrong with it. Maybe I'm not seeing something obvious? Is it a known bug? If so I couldn't find a corresponding issue. Is this an intended behaviour? This is incredibly frustrating:(

in Engine by (17 points)

I regularly call add_child() from nodes other than the node I'm adding the child to, and it works fine. Is the script you're calling it from a singleton or something? What type of node is get_parent() returning?

No, I don't call it from a singleton. I tried to parent to nodes of different types. It doesn't work with Node2D and Position2D - the ones that I actually need, because I need the parent node to have a position in space. It also doesn't work with Viewport. Interestingly, it works with Node, but I can't use Node in my case.

1 Answer

+4 votes

I got a different error, something about the node still setting up children.
Here's what you can use in this case:

            get_parent().call_deferred("add_child", c)
by (22 points)
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 webmaster@godotengine.org with your username.