How can I have more control when I add children?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulioYagami

When I add children by code, I see no reference to them. Like for example:

func _ready():
    var n = 0
    while n != 5:
        var i = something.instance()
        add_child(i)
        n += 1
    # And now I can not access any of that type of node?
:bust_in_silhouette: Reply From: SIsilicon
  1. You can access your nodes back by using get_child(node index in local node tree)
  2. You can make a variable to hold a reference to them while you’re making them. You could even use an Array or dictionary to hold multiple at once.

Can you give me a practical example of the second item?

JulioYagami | 2018-11-28 12:13

var nodeArray = []

func _ready():
    var n = 0
    while n != 5:
        var i = something.instance()
        add_child(i)
        nodeArray.append(i)
        n += 1

Now you have an array of the nodes you just created.

jandrewlong | 2018-11-28 15:25