Not sure I understand the rules behind early returns

Before reformatting to use early returns my code was working fine and was decrementing the lives as should be happening but now it will decrement my lives ONCE and only once to 4 then seems to be stuck there so I can not fail.

my OnInput that calls my Process guess looks like this

if(bGameOver){
        ClearScreen();
        SetupGame();
    }
    else {
        ProcessGuess(Input, Lives);  
    }

and then my ProcessGuess deviates but not in a way that should effect in this way looks like this

void UBullCowCartridge::ProcessGuess(FString Guess, int32 Lives){
        if (Guess == HiddenWord){
            ClearScreen();
            PrintLine(TEXT("You have won!"));
            EndGame();
            return;
        }

        --Lives;
        if(!Lives){
            ClearScreen();
            PrintLine(TEXT("You have lost all lives, womp womp"));
            PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
            EndGame();
            return;
        }

        if(Guess.Len() != HiddenWord.Len()){
            PrintLine(TEXT("Sorry try again"));
            PrintLine(TEXT("The hidden word is %i characters long"), HiddenWord.Len());
            PrintLine(TEXT("You have %i lives left"), Lives);
            return;
        }

        if(Guess != HiddenWord){
            PrintLine(TEXT("Sorry thats not the right word"));
            PrintLine(TEXT("You have %i lives left"), Lives);
            return;
        }
}

I subtract lives immediately after seeing that the guess is wrong that way I know all references to lives after that are using the updated number (in theory)

However in game, I guess once, I go down to 4 and can never get down to 3. Please help

Fixed it.
Because I had ‘Lives’ as part of my parameters my decrements kept pointing to the local variable ‘Lives’ that kept resetting back to 5 every time the return happened

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

Privacy & Terms