Currently trying to set a Game Over state, now when I set the value to be above 0 it enters the switch statement and restarts the level. If I set it <=0 it doesn’t enter the switch statement. Even If I do <10 it still doesn’t enter the statement. But If I use greater than it works again. Any Idea why this is happening? I’m new to all this and still learning. Thank you.
//FPSGameMode constructor
AFPSGameMode::AFPSGameMode(const FObjectInitializer& gameModeVar)
: Super(gameModeVar)
{
// set default pawn class to our Blueprinted character
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnObject(TEXT("Pawn'/Game/Blueprints/BP-FPSCharacter.BP-FPSCharacter_C'"));
if (PlayerPawnObject.Class != NULL)
DefaultPawnClass = PlayerPawnObject.Class;
//Use custom HUD class
HUDClass = AFPSHUD::StaticClass();
}
void AFPSGameMode::BeginPlay()
{
Super::BeginPlay();
SetCurrentState(EPlayerState::EPlaying);
StartMatch();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Welcome To Hell, press M for help"));
}
AFPSCharacter* MyCharacter = Cast<AFPSCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
if (MyCharacter)
{
if (MyCharacter->GetCurrentHealth() <= 0)
{
SetCurrentState(EPlayerState::EGameOver);
}
}
if (MyCharacter)
{
MaxHealth = (MyCharacter->GetInitialHealth());
}
if (HUDWidgetClass != nullptr)
{
CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), HUDWidgetClass);
if (CurrentWidget != nullptr)
{
CurrentWidget->AddToViewport();
}
}
}
float AFPSGameMode::GetMaxHealth() const
{
return MaxHealth;
}
EPlayerState AFPSGameMode::GetCurrentState() const
{
return CurrentState;
}
void AFPSGameMode::SetCurrentState(EPlayerState NewState)
{
CurrentState = NewState;
HandleNewState(CurrentState);
}
void AFPSGameMode::HandleNewState(EPlayerState NewState)
{
switch (NewState)
{
// If the game is playing
case EPlayerState::EPlaying:
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Playing");
}
break;
// If we've won the game
case EPlayerState::EWon:
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Won");
}
break;
// If we've lost the game
case EPlayerState::EGameOver:
{
AFPSCharacter* MyCharacter = Cast<AFPSCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
if (MyCharacter)
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Game Over!");
UGameplayStatics::OpenLevel(this, FName(*GetWorld()->GetName()), false);
}
}
break;
// Unknown/default state
default:
case EPlayerState::EUnknown:
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Unknown");
}
break;
}
}