#Spawn multiple enemies from the array?

4 messages · Page 1 of 1 (latest)

stiff saffron
#

I am working on a top down shooter game, and have a enemy_spawner Node with packed scenes array, which is being filled with new enemies (scenes). Every enemy has a enemy script attached to it.

Enemy spawner script:
var e = enemy_array.pick_random().instantiate()
get_parent().add_child(e)

What I wanna do is, I want to instantiate few enemies of one type at once (to have some alien formations), but I dont know how to duplicate specific scene, or how to refer to the element inside the array of scenes. I have tried:

enemy_array[1] and change the position or even duplicate the Node, also I have tried by modifying the enemy script attached directly to the enemy node, but I receive instantly an error telling me that the Packed scene is null, since its not still spawned or so.

What I also have tried in a first place is much easier option: to make a root node of this enemy and then add few enemies as a sub Nodes like an array of duplicated enemies, but it was hard to fix the area2d and collision shape issues so I have canceled this idea and tought it would be much easier to modify the spawner script only.

Is there a way to change the behavior of each enemy inside the spawner script or I have to make a new enemy script specially for these type of enemies (maybe duplicate the node a few times)? Thanks!

rotund rock
#

I dont know if i understand your problem well but, you want to spawn multiple enemies from that array at once? If so, you could just wrap that code inside a for loop to spawn as many as you need for enemy in 10 etc. If you want to immediatly do something with the spawned enemies, you can make a new array and add them all to it as they spawn spawned_enemies.append(e). And you can use that array to do whatever you need. In your case e is the enemy itself so you can do things with it right there as it spawns like call a function on it etc.

stiff saffron
#

Yes, but I want to do something with the specific enemy scene inside the array. For example I have 4 enemies inside the array but I want enemy2 inside the array when randomly chosen to be spawned 4 times at once and to have some formation like spawned as circle it doesnt matter, I just want to be able to make modification to the specific element inside the array before being spawned or immediately when spawned.

brisk sigil