Correctly remove an element of an array based which animation it is playing

Godot Version

4.5.1.stable

Question

Hey, still new and figuring stuff out. I created a small Vampire-Survivor-like game where the player automatically shoots the closest monster. This all worked until I added a spawn countdown for the monsters. I did that by giving the monsters a countdown animation and disabling any collision zones while the countdown is active. Problem is the player doesn’t differenciate between countdowns and actual monsters yet. I tried to sort out the unwanted monsters by the code below. All monsters are direct children of $Enemies and AnimatedSprite2D is a direct child of each monster. AnimatedSprite2D has three animations: “spawn_countdown”, “walk” and “hurt”. I even printed i.get_node(“An…2D”).animation and all three animations were possible outcomes. I have no idea why it isn’t working and any help is greatly appreciated, ty :slight_smile:

Don’t erase array elements while iterating through the array. It’ll mess up your iterator. Put deletion candidates in another array and then erase them, or more elegantly - use Array::filter()

Awesome ty <3
It worked using the filter

1 Like