+1 vote

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.

in Engine by (22 points)
edited by

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?

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.

1 Answer

+1 vote

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.resize(8)
arr[7] = "test"
print(arr[7])
by (5,240 points)

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

Results

FIXED

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.