I’m trying to get the level to end after all spawned enemies are destroyed. Below is the code that I have so far.
Hey @dragonshadow68! Welcome to the forums!
So we don’t really know what it is you want. Ask yourself, what do you want to happen specifically?
“end the level” right? Okay but what does that mean to you?
After you ask yourself that, figure out what parts are working and what parts are not working.
Bring that information to us! Because right now there’s no question to look for an answer to!
Yeah, it’s not clear what you’re asking. I will leave a few suggestions though.
If you’re incrementing the enemy count as you’re playing, you’ll run into a problem that you can destroy all the current enemies before the next enemies spawn. That means the enemy count will be zero very early on before all the enemies had a chance to spawn in. To avoid that, you need a proper end game flag. You need something to tell your code that no more enemies will be spawned. ONLY THEN should you check the enemy count.
For “ending the level”, I usually pause it. You can still display widgets. This lets me restart the level if I want, or unload the current level and load another one. My personal experience is that unloading and loading levels is a real PITA in UE. You’d think it’d be easier. You have to unpause, unload, wait for confirmation that it unloaded, then load new level, wait for confirmation, etc. I also had to manually remove all actors that were spawned in, but not destroyed (ie. if the player dies). But I’m in C++. I think in Blueprints, it might be a bit easier.
I want the ‘you win’ screen to appear after all the enemies are defeated. Kind of a win condition situation.
- Create a counter in your Game State and create an Event called End Game or something.
- In your Enemies’ Begin Play tell your Game state to register the enemy. The registration itself should increment the counter and bind a an event OnDeath that decrements the counter when that enemy dies. When decrementing you should check if it is 0 and if so call the End Game event.
- Wherever you handle your UI on begin play bind to the End Game even from the Game Mode. What you bind should tell your HUD to show the “You Win” screen.
It looks complex but it’s not.
Does your BP not work? What’s the problem?
BTW, I can’t stress enough that just having a counter won’t work. It will go to zero plenty of times before all enemies have finished spawning. You need a separate flag indicating if the last enemy has spawned or not. ONLY AFTER the last enemy has spawned should you check the enemy counter if it’s zero.
My BP doesn’t work and I’m not sure what to do to get it to work.
Put a breakpoint on the On Killed event. Is it triggering?
And don’t forget to the set the Enemy Count to zero when your level starts.
Game Mode handles the Rules of your game. It’s also the class that handles spawning of Pawns and controller possession of the Pawns (NPC → AI controllers etc).
As the GM spawns an NPC/Enemy have it increment an Int variable (Enemies Alive). As an Enemy dies have the GM deduct from the Enemies Alive variable. This will be your primary control variable for determining the end of game/round.
You’ll also need a variable for determining the max number of enemies to spawn per game/round.
Then it’s a matter of each time an enemy dies check if Max has been reached and Enemies Alive == 0. If true, call an end game event in the GM or the Game State. Have that event call an Event Dispatcher. Bind it in your controller which will handle the UI elements.
Is there a way that you could show me what that would look like?