Jan Seda
Jan Seda

Reputation: 25

Godot: Selecting scene in editor by its tool-created child

I have a @tool-annotated empty scene which extends Node3D that I use for marking NPC spawn locations. On ready when is_editor_hint I add a mesh instance with geometry so the location is visible in the editor. The added mesh gets displayed in the editor just fine and everything works as expected except that clicking the tool-created mesh (which is child of the instantiated scene) in the editor doesn't actually select the parent scene. If I add the very same mesh to the scene statically instead of on _ready, I can select the instantiated scene by clicking the mesh as I would expect.

Is there any way to make this "dynamically created child" clickable in the editor? Is there anything that the editor sets when adding a child node that doesn't happen when using add_child? I've tried reparenting or changing owner but nothing helped. Also the child's AABBs are correct.

For reference the Node3D scene's script goes like this:

func _ready() -> void:
    if Engine.is_editor_hint():
        var mesh_instance = MeshInstance3D.new()
        mesh_instance.position.y += 0.03
        mesh_instance.cast_shadow = false
        var qmesh: QuadMesh = QuadMesh.new()
        qmesh.size = Vector2(0.4, 0.4)
        mesh_instance.mesh = qmesh
        add_child(mesh_instance)

Thank you for any help, trying to find specific spawner in the tree can be really frustrating.

EDIT:

Solved by the issue linked in Bugfish' comment: Setting owner to self in the tool script makes it work as expected. The owner needs to be set AFTER the child is added.

add_child(mesh)
mesh.owner = self

seems to work, while

mesh.owner = self
add_child(mesh)

does not

Upvotes: 0

Views: 439

Answers (1)

Bugfish
Bugfish

Reputation: 1729

As posted in the comments:

After adding the child via a tool script, the owner of the nodes have to be set.

See the issue on github here

Upvotes: 2

Related Questions