My GameMode end condition check implementation. Thought I would share. I created a private member function:
#include <optional>
/// ...
/**
* Returns an optional with true if player wins, false if player loses. An empty optional indicates the game is not over.
*/
std::optional<bool> CheckEndGameCondition(APawn* PawnKilled) const;
Note I updated my Build.cs file to enable C++17 which is currently the latest version allowed by the engine (C++20 not yet supported as of 4.26.2 - most likely because one of the supported platforms doesn’t support it as VS 2019 does)
I also like to make use of the standard C++ algorithms where I can so opted to use std::any_of.
#include <algorithm>
// ...
std::optional<bool> ASinglePlayerEnemyHuntGameMode::CheckEndGameCondition(APawn* PawnKilled) const
{
AController* PawnController = Cast<AController>(PawnKilled->GetController());
if (PawnController && PawnController->IsPlayerController())
{
return false;
}
auto Begin = TActorIterator<AShooterAIController>(GetWorld());
auto End = TActorIterator<AShooterAIController>(EActorIteratorType::End);
// TODO: What if multiple players killed at the same time?
if (Begin == End)
{
UE_LOG(LogTemp, Warning, TEXT("No actors left in the level! Marking game as lost"));
return false;
}
bool AnyAIRemains = std::any_of(Begin, End, [](auto ShooterAIController) {
return !ShooterAIController->IsDead();
});
return AnyAIRemains ? std::nullopt : std::make_optional(true) ;
}
I then call it in PawnKilled with
auto GameOverOptional = CheckEndGameCondition(PawnKilled);
if (!GameOverOptional)
{
return;
}
UE_LOG(LogTemp, Display, TEXT("Game ended"));
EndGame(*GameOverOptional);