Entering into an If statement not working

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;
	}
}

Can you provide full code listing?

Can do ye.

As I see you compares GetCurrentHealth() <= 0 at start of game.
MaxHealth you changed as default value in blueprint?

So if statements returns ture you goes to SetCurrentState(EPlayerState::EGameOver);
And in the switch statement checks the incoming NewState parameter.

switch statement looks strange in your code, but seems valid, but I think this construction seems more briefly:

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;
    }

You can use debugger and step-by-step exec your code and look at variables to understand whats the occurs.
Watch the variables in debugger, look at MaxHealth variable before entering in “if” statement, and watch the param NewState after entering into method.

I thnk this is best method to understand what’s happening.

But I not seen any implicit problems in code.

Solution: use debugger in Visual Studio.

I think I know the problem, don’t know how to fix it yet but the problem of course, is it’s checking the health straight from the start which is 100. It should be checking whenever the character takes damage right?

I needed to have put in the tick function to keep checking the players health every frame. It now works! With my previous method, it just checked the health on the game starting so obviously it’s not going to enter the if function.

Facepalm

Thanks for the help man.