Early return

I am following the Unreal cpp course and I am on section 3-61 Early returns. I am experiencing an issue where the lines of code outside of the if statement inside of the ProcessGuess function does not run. For example the if (Guess.Len() != HiddenWord.Len()) and everything in {} runs however the Printline(TEXT("%i"), --lives); doesn’t so in the gameplay. Player’s life isn’t decremented even if I entered the wrong word.
I also tried copying the instructor’s code and running that but still experienced the same issue. Any help here would be greatly appreciated.

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (Guess == HiddenWord)
    {
        PrintLine(TEXT("You have Won!"));
        EndGame();
        return;
    }

    // if (!bIsIsogram)
    // {
    //     /* code */
    //     Prompt to guess again as not isogram.
    //     return;
    // }

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

    PrintLine(TEXT("Lost a life!"));
    PrintLine(TEXT("%i"), --Lives);

That’s what the code does. A return statement returns from the function i.e. exits it. Remember that Mike didn’t want to remove a life if a guess was invalid.

Ok got it. I missed that part.

1 Like

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

Privacy & Terms