Lives not decrementing

I’m pretty sure everything is correct here but for some reason, when the guess is incorrect, the lives value does not decrease so the value stays the same after each incorrect guess. No VScode errors or Unreal compilation issues.

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    int32 Lives = HiddenWord.Len();


    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else
    {
        if (Input == HiddenWord)
    {
        PrintLine(TEXT("You have won!"));  
        EndGame();
    }
    else
    {
        --Lives;
        PrintLine(TEXT("Lost a life!"));
        if (Lives > 0)
            {
            if (Input.Len() != HiddenWord.Len())
        {
            PrintLine(TEXT("Sorry, try again! \nYou have %i lives remaining."), Lives);
        }
        
     }
     else
     {
         PrintLine(TEXT("You have no lives left!"));
         EndGame();
        }
     }
 }

I noticed that ‘Lives’ is also declared in the SetupGame() function -

Not sure if that’s causing a problem?

void UBullCowCartridge::SetupGame()
{
    PrintLine(TEXT("Hi there! Welcome to the Bull Cow Game!"));

    HiddenWord = TEXT("Hidden");
    Lives = HiddenWord.Len();
    bGameOver = false;

Seems like I was declaring an int32 called Lives as HiddenWord().Len at the top of my first codeblock - I think that was from a previous example. I removed it and now it works.

Resolved!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms