The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+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():

    # 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 ;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.

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.