My code compiles without any errors, but when i press play unreal crashes

Hi,
My code compiles perfectly fine but when i press play unreal engine crashes instantly.The error says that it goes out of bounds but i cant see anywhere that would be out of bounds in my code. It would be very much appreciated if someone could tell me why UE is crashing and how i can fix it.

the error from the crash report i think is:
Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\Containers/Array.h] [Line: 614] Array index out of bounds: 0 from an array of size 0.

my code that i have just added is

    int32 Bulls, Cows;
    GetBullCows(Guess, Bulls, Cows);
    PrintLine(TEXT("You have %i Bulls and %i Cows"), Bulls, Cows);



void UBullCowCartridge::GetBullCows(const FString& Guess, int32& BullCount, int32& CowCount) const
{
    BullCount = 0;
    CowCount = 0;

    for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
    {
        if (Guess[GuessIndex] == HiddenWord[GuessIndex])
        {
            BullCount ++;
            continue;
        }

        for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
        {
            if (Guess[GuessIndex] == HiddenWord[HiddenIndex])
            {
                CowCount ++;
            }
        }
    }
}

and in BullCowCartridge.h i added this

	void GetBullCows(const FString& Guess, int32& BullCount, int32& CowCount) const

Your array is empty.

what array? from what i can see, everything i can see has a value to it.

  • Guess is the input, so that is the only thing that can be empty at the point of set up game.

  • Hidden word is chosen from begin game. but i know that it chooses a word as if i comment everything out that i wrote from this lecture then it chooses a word from HiddenWordList at random

  • both BullCount and CowCount are set to 0

I feel like i’m missing something obvious but i don’t know what

What’s your begin play and everything it calls?

This is my whole code

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

    Isograms = GetValidWords(Words);
    SetupGame();
}

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

void UBullCowCartridge::SetupGame()
{
    PrintLine(TEXT("Welcome to Bull Cows!"));

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

    PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
    PrintLine(TEXT("you have %i Lives"), Lives);
    PrintLine(TEXT("The Hidden Word is: %s.\nIt is %i characters long"), *HiddenWord, HiddenWord.Len());
    PrintLine(TEXT("Type in your guess\nPress enter to continue..."));
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("Press enter to play again."));
}

void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
    if (HiddenWord == Guess)
    {
        PrintLine(TEXT("You have won!"));
        EndGame();
        return;
    }
    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("Sorry, try guessing again!\nYou have %i lives remaining"), Lives);
        PrintLine(TEXT("The hidden word is %i letters long"), HiddenWord.Len());
        return;
    }

    if (!IsIsogram(Guess))
    {
        PrintLine(TEXT("No repeating letters, guess again"));
        return;
    }

    PrintLine(TEXT("You have lost a life"));
    --Lives;
    PrintLine(TEXT("You have %i lives remaining"), Lives);

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

    int32 Bulls, Cows;
    GetBullCows(Guess, Bulls, Cows);


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

bool UBullCowCartridge::IsIsogram(const 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;
}

TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& Wordlist) const
{
    TArray<FString> ValidWords;

    for (FString Word : Wordlist)
    {
        if (Word.Len() >=4 && Word.Len() <= 8 && IsIsogram(Word))
        {
            ValidWords.Emplace(Word);
        }
    }
    return ValidWords;
}

void UBullCowCartridge::GetBullCows(const FString& Guess, int32& BullCount, int32& CowCount) const
{
    BullCount = 0;
    CowCount = 0;

    for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
    {
        if (Guess[GuessIndex] == HiddenWord[GuessIndex])
        {
            BullCount++;
            continue;
        }

        for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
        {
            if (Guess[GuessIndex] == HiddenWord[HiddenIndex])
            {
                CowCount++;
                break;
            }
        }
    }
}

And what is the contents of Words?

#pragma once
#include "CoreMinimal.h"

const TArray<FString> Words =
{
TEXT("a"),
TEXT("ability"),
TEXT("able"),
TEXT("about"),
TEXT("above"),
TEXT("accept"),
TEXT("yourself")
};

There are 1000 terms inbetween, but i’ve just put the beginning and the end

Well everything looks correct to me. Could you try rebuilding by closing Unreal and then deleting the Binaries and Intermediate folder and then trying to open the project again.

1 Like

That has worked! thank you for all the help :slight_smile:

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

Privacy & Terms