This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I have a custom scene that I've built as a reusable dialog. I want any nodes added to an instance of that scene to be added to a descendant of the scene instead of the scene root. I have been able to get this partially working by overriding add_child in the scene's script:

extends Control

func add_child(child: Node, force_readable_name: bool = false, internal: int = 0):
  get_node("./button_container").add_child(child, force_readable_name, internal)

Any nodes added via the editor do show up in the scene preview, and if I enable "Editable Children" for the instanced scene I can see the new node in the tree (with a white label - all of the default nodes are in yellow). Once I save and reload the scene, the child node disappears. That says to me that the node isn't being placed into the tree properly, so it's not being serialized when I save the scene. I thought this may be due to the node's owner not being set correctly, but setting the child's owner to button_container or self doesn't change anything.

The dream state would be to have the children of button_container listed in the scene tree editor as the children of the instanced scene.

Godot version 4.0.beta12.official [3c9bf4bc2]
in Engine by (15 points)

1 Answer

+1 vote
Best answer

You're correct, the node here won't persist because add_child doesn't do this:

Rather, you want to use node.set_owner(get_tree().edited_scene_root).

by (1,406 points)
selected by

Along with this change, I found that I also have to enable "Editable Children" for the new nodes to be saved. Disabling "Editable Children" causes any nodes added to the instance to be removed (which the editor does warn you of).

I found that I could make this slightly easier on myself with the following (in the script on the root of the instanced scene):

func _ready():
  get_tree().edited_scene_root.set_editable_instance(self, true)

This enables "Editable Children" automatically when I add a new instance to another scene, but also collapses its children to reduce clutter. It's not perfectly ideal (I can still see all of the other nodes in the instanced scene when I expand the tree), but it will have to do.

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.