Hi,
I really hope someone can help. This problem is completely show-stopping for me.
I'm creating nodes with new(), then adding them to the hierarchy with addchild(), then using setowner() after that.
However, the nodes never show up in the editor (hierarchy), no matter what I do.
I know that they are being created, because by using printtreepretty(), it shows them there.
Also, the created nodes are destroyed after the game is closed. They don't stay in the hierarchy.
Here's the link to download the example project so you can see for yourself:
https://www.dropbox.com/s/hjufss85461tanq/Add%20Child%20Example.zip?dl=0
(Press the "Enter" key to run the function that creates child nodes, then read the output for information. It will create a child every time you press "Enter".)
If you don't want to download that file, here's the code from the project.
(Sorry for the formatting.)
The hierarchy Looks likes this:
--Main
----Holder
[Here's the Main node's script:]
tool
extends Node
var child_node : Node
const child_name : String = "Child"
var child_script : Script = preload("res://child_script.gd")
var holder_node : Node
const holder_name : String = "Holder"
func add_child_to_holder_node():
child_node = Node.new()
child_node.name = child_name
holder_node.add_child(child_node)
child_node.set_owner(self)
child_node.set_script(child_script)
print_tree_pretty()
func _ready():
holder_node = find_node(holder_name)
pass
func _input(event):
if event.is_action_pressed("ui_accept"):
add_child_to_holder_node()
[Here's the child_node's script:]
extends Node
var number_of_child_nodes : int
func _init():
number_of_child_nodes = get_parent().get_child_count()
print(" ")
print("child_script was successfully attached to created child_node.")
print("number_of_child_nodes = " + str(number_of_child_nodes))
print(" ")