Display amount of enemies remaining

What would the best way to be to display the remaining amount of enemies from a map ?

Create a variable on a gameistance or a gamemod. Then when an enemy dies or spawn decrement or increase that number. Then if you want to do a pretty thing, create an HUD and bind that variable to a text, or you can use a print string too.

You normally use the GameState for that type of information. Create a replicated var and update it from GameMode when your enemies spawns and dies.
As every player/client has access to the GameState you can retreive it there for UI.

I managed to complete it a different way . In the level blue prints i got all actors of class got my base enemy class promote that it a variable then got the length of that variable and used it to set the variable in the widget blueprints. It works well because every level will have different amount of enemies and it up dates on the fly

2 Likes

For a multiplayer approach without AI/bots (aka non-humanly controlled pawns):

With Teams:

  • Create a custom PlayerState to hold a team name/team tag and add a method bool IsPawnAlive().
  • This method will return true if the ControlledPawn is not destroyed or any criteria that works best for you. To access the PlayerController, cast the PlayerState->GetOwner().
  • Create a Method inside GameState to return the number of enemies, and pass your team tag/name as parameter. ex.: int GetRemainingEnemies(FName YourTeam)
  • The method should iterate through every PlayerState inside the PlayerArray, cast to your custom Player State and count each one that is different from *YourTeam *AND *IsPawnAlive() *is true, then return the counter.

Without Teams:

  • Create a custom PlayerState and add a method bool IsPawnAlive().
  • This method will return true if the ControlledPawn is not destroyed or any criteria that works best for you. To access the PlayerController, cast the PlayerState->GetOwner().

Create a Method inside GameState to return the number of enemies. ex.: int GetRemainingEnemies()

  • This method will iterate through every every PlayerState inside the PlayerArray, cast to your custom Player State and count each one that *IsPawnAlive() *is true, then return the counter.

I would only suggest you to control the number of enemies in a variable if you are pretty sure you will synch it properly througout the game loop, otherwise not a good idea…


If you use AI/bots, you can use this local variable approach but make sure to update the counter when an enemy enters and dies. Or, you can use GetAllActorsOfClass and filter to your enemy classes/tags… but its way more expensive.

[QUOTE="


If you use AI/bots, you can use this local variable approach but make sure to update the counter when an enemy enters and dies. Or, you can use GetAllActorsOfClass and filter to your enemy classes/tags… but its way more expensive.[/QUOTE]

Can you elaborate on how it is more expensive

If you use an counter, then the cost is just 1 operation.

If you use GetAllActorsOfClass or GetAllActorsWithTag then:
1 - iterate through all actors in the game;
2 - save the matched references in an array;
3 - determine the size of the array.
So it’s not only expensive in terms of cpu but also in terms of memory,

It’s not all actors in the game just the level and if a level only contains say 20 enemies would that be an issue and is there a way to check the stats ?

There is nothing wrong in using those expensive functions. Except in those typical top down shooters where dozens of enemies die in just a few seconds (if you are calling them everytime an enemy dies).

it depens from you.
EDIT:EvilCleric just wrote with me xD