Help! Unreal Engine crash (array Index out of bounds)

The code compiles fine, upon attempting to play the game it crashes and produces this error message:

I have closed UE, deleted the Binary and Intermediate files, and attempted the play the game again but was met with the same crash report.
What do I need to do to get my game to play again?

BullCowCartridge.cpp:

#include "BullCowCartridge.h"
#include "HiddenWordList.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();

    SetUpGame();

    PrintLine(TEXT("The number of possible words is %i."), Words.Num());
    PrintLine(TEXT("The HiddenWOrd is: %s."), *HiddenWord); // Debug Line
    
    for (int32 Index = 0; Index != 10; Index++)
    {
        if (Words[Index].Len() >=4 && Words[Index].Len() <=8)
        {
        PrintLine(TEXT("%s"), *Words[Index]);
        }
    }
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    if(bGameOver)
    {
        ClearScreen();
        SetUpGame();
    }
    else //checking PlayerGuess
    {
        ProcessGuess(Input);
    }
}

void UBullCowCartridge::SetUpGame()
{
    PrintLine(TEXT("Greetings Peasant, to the Bull Cow game!\n"));
   
    HiddenWord = TEXT("handle"); 
    Lives = HiddenWord.Len(); 
    bGameOver = false;

    PrintLine(TEXT("You must guess the %i letter\nHIDDEN WORD correctly!\n"), HiddenWord.Len());
    PrintLine(TEXT("You have %i lives remaining"), Lives);
    PrintLine(TEXT("Type in your guess \nand press ENTER to continue!\n"));

}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;

    PrintLine(TEXT("\nPress Enter to play again!"));
}

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (Guess == HiddenWord)
    {
        PrintLine(TEXT("Well done, you win!"));
        EndGame();
        return;
    }
        // prompt to Guess AGain
        // Check Correct Number Of Characters
    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("The hidden word is %i letters long"), HiddenWord.Len());
        PrintLine(TEXT("You have %i lives remaining"), Lives);
        return;
    }
            // Check If Isogram
    if (!IsIso(Guess))
    {
        PrintLine(TEXT("You should not have repeating letters,\nguess again"));
        return;
    }
        // prompt to Guess AGain
        // Remove Life
    PrintLine(TEXT("You lost a life!\nPress enter and guess again..."));
    --Lives;

        // Check Lives > 0
        // IF Yes GuessAgain
        // If No Show Game Over and HiddeWord
        //Show Lives Remaining
        // Prompt To Play Again, Press Enter To Play Again
        // Check User Input
        // Play Again or Quit

    if (Lives <= 0)
    {
        ClearScreen();
        PrintLine(TEXT("You have no lives left!"));
        PrintLine(TEXT("The hidden word was %s."), *HiddenWord);

        EndGame();  
        return;  
    }       
    // show player the bulls and the cows
    PrintLine(TEXT("Guess again, you have %i lives"), Lives);
}

bool UBullCowCartridge::IsIso(FString Word) const
{

    for (int32 Index = 0; Index < Word.Len(); Index++)
    {
        for (int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)
        {
            if (Word[Index] == Word[Comparison])
            {
                return false;
            }
        }      
    }

    return true; 
}

HiddenWordList.h:

#pragma once
#include "CoreMinimal.h"

const TArray<FString> Words =
{
TEXT("attorney"),
TEXT("audience"),
TEXT("author"),
TEXT("authority"),
TEXT("available"),
TEXT("avoid"),
TEXT("away"),
TEXT("baby"),
TEXT("back"),
TEXT("bad"),
TEXT("bag"),
TEXT("ball"),
TEXT("bank"),
TEXT("bar"),
TEXT("card"),
TEXT("care"),
TEXT("career"),
TEXT("carry"),
TEXT("case"),
TEXT("catch"),
TEXT("cause")
};

BullCowCartridge.h:

#pragma once

#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"

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(FString Guess);
	bool IsIso(FString Word) const;
	TArray<FString> Words;
 
	// Your declarations go below!
	private:
	FString HiddenWord;
	int32 Lives; 
	bool bGameOver;
};
1 Like

Update: Problem solved!
I had overdefined the TArray by including it in my Public section within the BullCowCartridge header file.

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

Privacy & Terms