Instantiating a child node as part of an array Howto?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kc
:warning: Old Version Published before Godot 3 was released.

I am trying to instantiate a node as part of an array.
I want to do this like this because i want to have it as an array, and because i want to be able to create arrays.

Unfortunately I clearly have no idea what I am doing.

Here i the code_

    extends Node

func _ready():

	# Called every time the node is added to the scene.
	# Initialization here

	# Intantiate a bunch of dot.tscn 	
func makeDots():
# make an array and step through the array to create a bunch of different dots.
# in this case the dots have information as to where to go when instantiated
# new_dot needs to defined as an array
	var new_dot = []
		# to preload a scene as a constant
	var dots = preload("res://dot.tscn")
	# iterate through the array a random amount, instantiating each loop
	var i = 0
	var randomNumber = randi()%50+4#limit number with modulo (%)
	while i < randomNumber:
		new_dot[i] = dots.instance()
		# to add the new instance as a new child node 
		print("we made a dot")
            i+=1 #edit was not looping... without this line... 

would somebody be kind enough to explain what I am doing incorrectly.
My question should be also marked as Clueless, newbie.

I have spent a while researching how one might perform this activity, but i cannot understand what i am doing incorrectly.

Thanks for helping.

in regards to the comments below.

Firstly, thank you for helping.
It is a long and driven path to learn how to write halfway decent script (even) and time is limited, so for your help I thank you again.

randomize() is used to initialise the random seed to make sure that whenever the function is called (assuming that it might be used again perhaps in the same scene, more than once) the random seed is reset.

(from the docs)

Nil randomize ( )

Reset the seed of the random number generator with a new, different one.

Why put nodes in an array?
I am not sure, but i want to do it anyway :stuck_out_tongue_winking_eye:

What do I want to keep in the array?
I want to keep autonomous agents in the array,
which are instances of another scene.
in this case, a sprite with behaviour.

I see in your code you are instancing nodes and putting them in an array. But I don’t understand what you want to do in the first place, beyond that? What is randomization for?

Zylann | 2017-01-25 22:45

Same question that @Zylann asks you

I the first error I see is that you can not modify an index that does not exist, the array is empty new_dot[i] = dots.instance() maybe it should be new_dot.push_back(dots.instance()), Maybe it should be like this, I still see errors, what do you want to keep in the array? That’s all for now.

Aquiles | 2017-01-25 23:17

:bust_in_silhouette: Reply From: avencherus

If you want to use arrays in that manner, with assignments by index. You have to resize them first, so that the indexes exist.

var arr = []
arr[7] = "test"

Thanks. I will work with this and report any results.



my problems with making a while loop without an i+=1 iterator pretty glaring mistake.

kc | 2017-01-26 13:00