Topic was automatically imported from the old Question2Answer platform.
Asked By
klaas
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.
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