Kill all instances of an enemy when a switch is hit

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

Hi ! I’ve been trying to make a loot that kills all the enemy in the scene when touched and i cant figure out how.
All my enemies are spawning from a spawner. they are all kinematicbodies2d.

Someone has some ideas or queue lines …?

Thanks a lot !

:bust_in_silhouette: Reply From: timothybrentwood

When you are instance()-ing and add_child()-ing your enemies also:

var new_enemy = enemy_scene.instance()
self.add_child(new_enemy)    
new_enemy.add_to_group("enemies")

Then when you pick up that item you can:

for enemy in get_tree().get_nodes_in_group("enemies"):
    enemy.die()

alternatively:

get_tree().call_group("enemies", "die")

Replace die with whatever function you use to kill your enemies normally.

Thank you so much, it works and I learned the concept of group !

dany_pitre | 2021-05-06 23:53