0 votes

Hello everyone!
I`ve encountered a problem while writing my plugin. I want, on a press of a button, to add a new custome type node and move the currently selected node under the new node.

        var zone = myCustomType.new()
    var parent = node.get_parent()
    zone.name = node.name+"Zone"
    parent.add_child(zone)
    zone.owner = parent
    parent.remove_child(node)
    zone.add_child(node)

The result is ... the new node has entered the tree correctly but the old node does not appear under the new one. The old one is just gone in the tree (still there in the viewport).
I've tried to change the owner of the old node to the new one, but that did not help either.

Does anyone encounter similar problems?

in Engine by (3,940 points)

1 Answer

0 votes

For anyone who has the same problem. This is the solution.

The problem is quit complex.
When you insert the new node you first have to assign it to a parent, then set the owner afterwards. But now the new node has not entered the tree yet. So you have to wait until this has happend.
Then you can add the old node as child. But the new node does not have an owner till then. If you do so, the old node looses its owner. Now you have to reasign the owner.

Ooof!!

func _add_child_deferred(node,child):
    var owner = child.owner
    child.get_parent().remove_child(child)
    node.add_child(child)
    child.owner = owner

func _on_CreateZones_pressed():
    var nodes = dzPortals.plugin.get_editor_interface().get_selection().get_selected_nodes()
    for node in nodes:
            var zone = dzPortals.plugin.dzPortalsZone.new()
            var parent = node.get_parent()
            var name = node.name+"Zone"
            var _owner = node.owner
            zone.connect("ready",self,"_add_child_deferred",[zone,node],CONNECT_ONESHOT )
            zone.name = name
            zone.translation = node.translation
            parent.add_child(zone)
            zone.owner = _owner
            node.translation = Vector3.ZERO

If anyone has a more elegant solution for this i would appreciate to hear from you

by (3,940 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 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.