How to instance for every count of an element inside an array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sugor

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

:bust_in_silhouette: Reply From: MrEliptik

Well, you can use the count function of Array: Array — Documentation de Godot Engine (4.x) en français

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

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()
		self.get_node("Elements").add_child(ignis)
		ignis.transform = get_parent().global_transform

but I’ll try yours next…

Sugor | 2021-05-22 10:57