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():
set_process(true)
# Called every time the node is added to the scene.
# Initialization here
makeDots()
#makeDots()
# 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
randomize()
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
add_child(new_dot[i])
print("we made a dot")
i+=1 #edit was not looping... without this line...
pass
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.
EDIT:
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 ;P
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.