+1 vote

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) ?

in Engine by (126 points)
edited by

2 Answers

+4 votes
Best answer
var enemies = get_tree().get_nodes_in_group("enemies")
for enemy in enemies:
    enemy.queue_free()
by (27,898 points)
selected by

Thanks for answering :)

+5 votes

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:
http://docs.godotengine.org/en/stable/classes/class_scenetree.html#numeric-constants

by (7,866 points)

This should definitely be the accepted answer.

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 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 webmaster@godotengine.org with your username.