Counting enemy object in room

I’m trying to learn GDevelop and game development in general by recreating the original Legend of Zelda for NES.

I’m trying to create a situation in a dungeon room in which when all the enemies in the room are dead, a sound will play and an item will appear at a certain location. I’m having trouble getting this to work using object picking.

I have room objects “CameraRoom” with an instance variable RoomID to identify a specific room. Then, I’m trying to pick all the enemies in the DungeonEnemies group which are in collision with that specific instance of the room object. When the enemy HP=0, I’m trying to add 1 to a scene variable to keep track of the total number of enemies killed in the room. Then, when that number reaches 3, I want a sound to play and an object to appear in the middle of the room. I’ve added a screenshot of the events I have which aren’t working.

Can someone please look and tell me what I’m doing wrong here? I’ve looked at another similar topic on the forum and tried to model my events after what was said there, but I just can’t seem to figure it out. Can I please get some help here? Any help is greatly appreciated!!!

You can “filter” or pick the objects with condition(s) and then check the number of instances currently picked.

Either way works, the 2nd way doesn’t require you to know how many zombies are in the room. Either count the dead or count the living. You can use any condition or variable to do the picking.

Revised: Sorry about that. I thought I had it right. This works

image

2 Likes

Keith_1357, thank you very much for your reply. Unfortunately, after trying the code you posted, I’m still not getting it to work. When I kill the three enemies in the room (RoomID=1), the sound doesn’t play which means it still isn’t working the way it’s supposed to. Here is my updated code:

So I’ve picked all Blue Keese enemies (which I also have as part of the DungeonEnemies object group). I’ve then filtered the objects by being in collision with CameraRoom, variable RoomID=1. I have the Keese dying when I hit them, which means their HP variable value is less than or equal to 0. Then I have the “Number of currently picked” set to 3, and 3 Blue Keese in collision with CameraRoom, variable RoomID=1. I take it to mean that when all 3 Blue Keese in Room 1 are dead, the sound “Key Appear” should be playing, but it isn’t. Nothing happens at all.

What am I missing here? It feels like the logic is correct, but am I wrong in thinking that?

Maybe one of your variables aren’t the values they need to be. This works for me. Click the “bat” to subtract 50 HP. The objects are draggable.

I linked and stuck a text object with the sticker behavior just to show the values.

1 Like

Here’s an example with the reverse invert not condition that just checks if all are not alive. You can drag the “bats” in and out the room.

1 Like

How are you applying the damage? Are you using a variable or the health behavior?

With the health behavior on the bats, it would be something like.

1 Like

General reminder, anything that deals with playing sounds should always include something that acts as a trigger once, either the trigger once condition itself, or a boolean that is flipped as part of the action.

Outside of that, I also am not seeing any issues when I check Keith’s suggestion (with NOT or with an inverted condition)

2 Likes

I’m still having no luck. Here is my new code:

I’m using an object variable (HP) for the Blue Keese enemy. When I hit the Blue Keese with the sword, it triggers the events for its death, seen in the bottom block of code. That tells me that it is working as it should. When I run the game with everything besides the HP=0 of Blue Keese condition, the sound effect plays immediately, which tells me it is detecting 3 Blue Keese in Room 1. However, when I add the HP=0 condition as shown and kill all three bats, nothing happens…the sound doesn’t play. For what it’s worth, the variable HP of Blue Keese is set to 1 by default, meaning it dies in 1 hit from any weapon.

I double checked the CameraRoom instance variable to make sure everything is correct there, and it is…the instance for Room 1 is set to RoomID=1. It seems to be counting the enemies correctly, hence the sound playing when I remove the HP-related condition. The problem seems to be in detecting that the 3 enemies are dead, despite it carrying out the actions under the “Enemy Death” group at the bottom of my code.

Any ideas? Everything you’re saying makes perfect sense, and I GREATLY appreciate the effort you have put into helping me thus far. I feel like there is just one key little thing I’m missing somewhere and I can’t identify what it is.

By the way, I have an external event sheet for the Dungeon Enemies I created to handle the item drops of enemies, just to try to keep things clean. Here is the code snippet of that:

I wouldn’t think that this is somehow conflicting with this whole thing working, but I thought I would include it and mention it just in case. Any ideas where I might be going wrong here? Thank you so much!

If you’re deleting the objects then the variable won’t be there to check. You could check the remaining objects in scene or inverted NOT in collision with the room.

It worked! Thank you so much Keith!

Question: If in a different room, I want only certain enemies of the DungeonEnemies object group to serve as the “trigger enemies”, which when all of these enemies in the room are killed, for something to happen…would you recommend creating a boolean variable for all DungeonEnemies object such as “TriggerEnemy”, then set it to True on each instance of the enemies which you want to be the trigger enemies? Is that how you would approach that? This way, since some dungeon enemies can’t be killed, it would filter those out for triggering an item to appear, door to open, etc when all the “Trigger Enemies” are dead.

If so, in the code you posted above, would I just add the boolean TriggerEnemy=true below the RoomID condition? Thank you so much for all your help!!!

Edited: There’s probably a better way but IDK. I changed the last part and added 2 possible condition groups. When there’s no objects then you can’t check the variables. So, I added a simple if there is nothing in collision.

I will give this a try and let you know if I run into any issues. Thank you so much again for all your help. This community is great!

1 Like

You’re welcome. Let me know. I’ll be around later. I hope this fixes it.