cleaning up enemies

I’m making a game in which there are enemies and transitions to new levels, but I don’t know how to make a check that looks at whether all the enemies were killed and opened a passage for us to a new level

Hey @PaNikcom! Welcome to the forums!

I suggest you create an actor to handle this! Just create Actor, no static mesh or anything, I’d name it “Enemy Manager BP” and place it into the level.

Make a custom event in it called “Enemy killed”.

Then once you’ve done that, off of begin play, “Get All Actors of Class” with your Enemy BPs as the input. That will give you an array, take that and use “LEN” to get how many actors there are. Right click the output Int, and promote it to a variable, let’s say “Enemy count”.

Now off of “Enemy Killed”, Get “Enemy Count”, --, Set Enemy count, then get enemy count and use = int with one as “0”, plug that into a branch to check T/F, and if true, open your door. You can do this by making a variable of the type of BP that is your door, making it public, and dragging the door actor into the slot you created by doing so while creating the level, linking the two! So now you can just “Get Door” and run whatever function you need to open it.

On the Enemies themselves, you’ll want to make an actor object variable of the BP you just created, called “EnemyManager”. Make it public (make the eyeball open!) and compile. Now, when you drop them in the level, make sure you’re setting each one to use their dedicated manager. On death, before destroying the enemy, have it “Get Enemy Manager” → Call “Enemy Killed” to reduce the “Enemy Count” number on your invisible manager actor.

I know it’s a lot, but you’ll figure it out and it’ll be so neat once you see it all snap together! :slight_smile:

1 Like

I think, if I were you, I’ll do that @Mind-Brain suggested, except several details:

  • move “EnemyManager” to Game mode (or GameState) class to be sure it’s initialized before enemies.
  • create a function to register enemy spawn (let’s name it “On Enemy Spawn”), where you’d be able to increment “Enemy Count”. Or you can just make “Enemy Count” public as suggested earlier.
  • at BeginPlay event in every enemy use “Get Game Mode” (or “Get Game State”), cast to your Game Mode (your Game State) class using “Cast to” node and call “On Enemy Spawn” (or directly increment “Enemy Count” if you didn’t made any functions).

The difference is what with my modifications every enemy informs your “EnemyManager” on their spawn, but without modifications your “EnemyManager” searches for enemies only at spawn (then BeginPlay at “EnemyManager” fired), so if you have several waves of enemies, which spawns during battle, “EnemyManager” won’t register them and your door opening or transition to other level will be processed too early (or “Enemy Count” become negative).
Maybe it looks very complicated, but… I think you can handle this, just a bit learning.

1 Like