0 votes

I need help on how to instance an object for every count of an element inside an array.
for example: I have an array [1,1,3] so element 1 count is 2 inside of an array so it will instanced it 2 times. So something like instanced based on an array like instance 2 bullet if there's 2 of number 1 inside an array and if there's no element there queue_free the instanced object

Godot version latest
in Engine by (46 points)
edited by

1 Answer

0 votes

Well, you can use the count function of Array: https://docs.godotengine.org/fr/stable/classes/class_array.html#class-array-method-count

This will return the number of time your value is present in the array. If you need more direction let me know.

For example:

for i in range (9):
    var to_instantiate = array.count(i)
    if var_to_instantiate == 0: continue # go to next iteration
    for x in range(var_to_instantiate):
        # instantiate your stuff here
by (631 points)

I kinda used something like this:

for i in range(contain_elements.size()):
    i = elements[0]
    if contain_elements.count(i) < 3:
        var ignis = preload("res://Entities/Player/Scenes/THREE/Ignis.tscn").instance()
        ignis.transform = get_parent().global_transform

but I'll try yours next...

connected 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.