Having Issues with the Final BullCow Game

I have an error and I don’t know why. I have searched for the possible issue, but I really can’t find what is causing it. In my BullCowCartridge.h, I am getting an error with the GENERATED_BODY() saying:
this declaration has no storage class or type specifier

here is the full code:

#pragma once

#include "CoreMinimal.h"

#include "Console/Cartridge.h"

#include "BullCowCartridge.generated.h"

struct FBullCowCount 

{

    int32 Bulls = 0;

    int32 Cows = 0;

};

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))

class BULLCOWGAME_API UBullCowCartridge : public UCartridge

{

    GENERATED_BODY()

    public:

    virtual void BeginPlay() override;

    virtual void OnInput(const FString& Input) override;

    void SetupGame();

    void EndGame();

    void ProcessGuess(const FString& Guess);

    bool IsIsogram(const FString& Guess) const;

    FBullCowCount GetBullCows(const FString& Guess) const;

    TArray<FString> GetValidWords(const TArray<FString>& WordList) const;

    // Your declarations go below!

    private:

    FString HiddenWord;

    int32 Lives;

    bool bGameOver;

    TArray<FString> Isograms;

};

You should just be able to ignore that. If it compiles then there’s no error.

You are right. It compiled fine, however I am having another issue. When I press play, Unreal freezes, then crashes with this error in the crash log:

Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:C:\Program Files\Epic Games\UE_4.22\Engine\Source\Runtime\Core\Public\Containers/Array.h] [Line: 611] Array index out of bounds: 0 from an array of size 0

UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_BullCowGame_1838!DispatchCheckVerify<void,<lambda_9dd96bc8550165faf18a28f37bc08630> >() [C:\Program Files\Epic Games\UE_4.22\Engine\Source\Runtime\Core\Public\Misc\AssertionMacros.h:162]
UE4Editor_BullCowGame_1838!UBullCowCartridge::SetupGame() [C:\Users\budgi\OneDrive\Desktop\GameFolder\UdemyUnrealClass\BullandCowGame\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp:32]
UE4Editor_BullCowGame_1838!UBullCowCartridge::BeginPlay() [C:\Users\budgi\OneDrive\Desktop\GameFolder\UdemyUnrealClass\BullandCowGame\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp:10]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll

Locating the lines it states, I think it has to do with me setting the HiddenWord to a random word using my TArray called Isograms at lines 10 and 32.

Here is my code for the BullCowCartridge.cpp

#include "BullCowCartridge.h"

#include "HiddenWordList.h"

void UBullCowCartridge::BeginPlay() // When the game starts

{

    Super::BeginPlay();

    

    SetupGame();

    Isograms = GetValidWords(Words);

    PrintLine(TEXT("The Word is: %s"), *HiddenWord);

    PrintLine(TEXT("There are %i possible words"), Words.Num());

    

}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter

{

    if (bGameOver)

    {

        ClearScreen();

        SetupGame();

    }

    ProcessGuess(Input);

}

void UBullCowCartridge::SetupGame()

{

    //Welcome Player

    PrintLine(TEXT("Welcome to the game of BullCow!"));

    HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() -1)]; 

    Lives = HiddenWord.Len(); 

    bGameOver = false;

    //Instructions

    PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len()); 

    PrintLine(TEXT("You have %i lives, or chances to guess"), Lives);

    PrintLine(TEXT("Please write your guess and \nthen hit the \"Enter\" Key to continue...\n"));

}

void UBullCowCartridge::EndGame()

{

    bGameOver = true;

    PrintLine(TEXT("\nThe game is over! Hit ENTER to play again"));

}

void UBullCowCartridge::ProcessGuess(const FString& Guess)

{

    if (Guess == HiddenWord)

        {

            ClearScreen();

            PrintLine(TEXT("Wonderful Job! You have guessed the correct word!"));

            EndGame();

            return;

        }

    //Checking Player Guess

    

    if (Guess.Len() != HiddenWord.Len()) //Check if Guess is right length

        {

            PrintLine(TEXT("Your guess is not even the right length!! \nThe Word is %i characters long"), HiddenWord.Len());

            PrintLine(TEXT("Guess Again"));

            return;

        }

        

    --Lives;

    PrintLine(TEXT("You have lost a life"));

    if (!IsIsogram(Guess)) //Check if Isogram

        {

            PrintLine(TEXT("No Repeating Letters"));

            PrintLine(TEXT("Guess Again"));

            return;

        } 

    //Show Player Bulls and Cows

    FBullCowCount Score = GetBullCows(Guess);

    PrintLine(TEXT("You have %i Bulls and %i Cows"), Score.Bulls, Score.Cows);

    

    if (Lives <= 0)//Check if Lives < 0

        {

            ClearScreen();

            PrintLine(TEXT("You are out of Lives"));

            PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);

            EndGame();

        }

        else 

        {

            PrintLine(TEXT("Sorry, that is incorrect, try again."));

            PrintLine(TEXT("You have %i Lives left"), Lives);//Show lives left

        }

}

bool UBullCowCartridge::IsIsogram(const FString& WordGuess) const

{

    int CharCheckNum = 0;

    while (CharCheckNum != WordGuess.Len() - 1) //will repeat until the CharCheckNum == to Length of WordGuess

    {

        if(WordGuess[CharCheckNum] == WordGuess[CharCheckNum + 1]) //will check if two letters are equal

        {

            return false;

        }

        ++CharCheckNum;

    }

        

    return true;

}

TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& WordPossibilities) const

{

    TArray<FString> ValidWords;

    

    for (FString Word : WordPossibilities)

    {

        if (Word.Len() >= 4 && Word.Len() <=8 && IsIsogram(Word))

        {

            ValidWords.Emplace(Word);

        }

    }

    return ValidWords;

}

FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const

{

    FBullCowCount Count {0, 0};

    for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)

    {

        if(Guess[GuessIndex] == HiddenWord[GuessIndex])

        {

            Count.Bulls++;

            continue;

        }

        for(int32 CheckIndex = 0; CheckIndex < HiddenWord.Len(); CheckIndex++)

        {

            if(HiddenWord[CheckIndex] == Guess[GuessIndex])

            {

                Count.Cows++;

                break;

            }

        }Preformatted text

    }

    return Count;

}
SetupGame(); // 1
Isograms = GetValidWords(Words); // 2

At 1, Isograms is empty as it’s populated in 2. Now consider what’s going to happen here in SetupGame

HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() -1)]; 

oh yes! I need to be more aware of the functions and where I place them… thank you! It is now working and I am excited to move onto the next Game!

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

Privacy & Terms