+5 votes

When I create a node from a tool script, it doesn't appears in the scene tree, and is not saved. How can I make it visible and/or saved?

in Engine by (28,740 points)

1 Answer

+7 votes
Best answer

You have to set the new node's owner as the scene root. When you save a scene the editor saves the root node itself and every node it owns.

my_node.set_owner(scene_root)

by (801 points)
selected by

I tried this:

mesh_instance.set_owner(get_tree().get_root())

but it produces an error:

ERROR: Node::is_editable_instance: Condition ' !is_a_parent_of(p_node) ' is true. returned: false
At: scene\main\node.cpp:1441

It should be the root of the scene, not the root Viewport. Something lilke:

mesh_instance.set_owner(get_tree().get_edited_scene_root())

Is this valid on runtime / exported game?
I guess it should be like this, I guess:

if get_tree().is_editor_hint():
    mesh_instance.set_owner(get_tree().get_edited_scene_root())

The editor hint check is needed, I think. You can use get_tree().get_current_scene() in the game.

Actually I think in game there is no need to declare an owner because it's just like instancing any node.

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.