Godot Version
v4.2.2.stable.official [15073afe3]
Question
Hi y’all,
I am working on an encounter system. When the player enters a room, I spawn enemies under a node called “EncounterHandler” for a given amount of time. The nodes spawned belong to a group called “enemies”. I am trying to determine how to programmatically find when all enemies have been defeated.
Every time the player defeats an enemy, I have the enemy node script call queue_free
. I figured I could check via signal from the enemy node in the encounter script like this:
if (self.get_children().any(func(node): node.is_in_group("enemies") != true)):
encounter_active = false;
But that did not work. Then I thought, maybe this is because the enemies are queued to be freed, but have not left the scene! Or, because I connect on then “child_exiting_tree” signal, they are still in the tree when the signal fires.
My current workaround is to check if the node in the group “enemies” has any nodes that are NOT queued for deletion to detect that combat is continuing.
Is there a better way to approach this?
Working code:
func check_remaining_enemies():
var arr = self.get_children()
var enemies_remaining = false
for child in arr:
if (child.is_in_group("enemies") and child.is_queued_for_deletion() != true):
enemies_remaining = true
if (enemies_remaining != true):
encounter_active = false
GlobalReferences.player_reference.move_camera_to_default_mode()