(ANSWERED) How can I delete all instances in certain group?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PugMasterPug
:warning: Old Version Published before Godot 3 was released.

I want that in certain moment all enemies (Which are in a group) get deleted.
I could create a global variable and detect it from the enemy script.
So when it changes it deletes the enemy.

But is there any easier way to delete all objects in a group (All objects are instances of a scene using the preload method) ?

:bust_in_silhouette: Reply From: Zylann
var enemies = get_tree().get_nodes_in_group("enemies")
for enemy in enemies:
	enemy.queue_free()

Thanks for answering :slight_smile:

PugMasterPug | 2017-05-11 13:02

:bust_in_silhouette: Reply From: eons

call_group may help

get_tree().call_group(SceneTree.GROUP_CALL_DEFAULT,"enemy","queue_free")

Or use the enemies delete/kill method if you need to do something else instead of free directly.

Documentation about groups:
http://docs.godotengine.org/en/stable/learning/step_by_step/scripting_continued.html#groups

SceneTree:

This should definitely be the accepted answer.

TheSecurityDev | 2020-02-07 16:43