No error in compiler, but UE is crashing when game launches

I tried implementing the code from this lecture and did not get an error, but UE keeps crashing when I try launching the game.

Here is the .cpp file

#include "BullCowCartridge.h"
#include "Math/UnrealMathUtility.h"

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

    Isograms = GetValidWords(Words);

    GetValidWords(Words);
    const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");
    FFileHelper::LoadFileToStringArray(Words, *WordListPath);
    //Words.RemoveAll([this](auto& word) {return IsIsogram(word); });
    SetupGame();

    
    
    
}

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

void UBullCowCartridge::SetupGame()
{
        //Welcoming the Player
    PrintLine(TEXT("Welcome to the 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 start off with %i lives."), Lives);
    PrintLine(TEXT("The Hidden Word is: %s."), *HiddenWord);//Debug Line
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    ClearScreen();
    PrintLine(TEXT("\nWould you like to play again?\nIf so press 'Enter' to play again!"));
}

void UBullCowCartridge::ProcessGuess(const FString& Guess)
{    
    if (Guess == HiddenWord)
    {
            PrintLine(TEXT("Congrats, you guessed the right word!"));
            EndGame();
            return;
    }
    
    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("The word is %i letters long \nyou have %i lives left..."), HiddenWord.Len(), Lives);
        return;
    }

    // Check for isogram
    if (!IsIsogram(Guess))
    {
        PrintLine(TEXT("No repeating letter \nguess again..."));
        return;
    }
    --Lives; 
    
    if (Lives <= 0)
    {
        PrintLine(TEXT("It seems you have run out of lives"));
        PrintLine(TEXT("The Hidden word was %s."), *HiddenWord);
        EndGame();
    }

    if (Lives > 0)
    {
        int32 Bulls, Cows;
        GetBullCows(Guess, Bulls, Cows);

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

        PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
        return;
    }
}

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;
}

//Limiting the words in the wordlist to just the valid ones
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 every index guess is same as index of hiddenword, BullCount++
    //If not a bull was it a cow? if yes CowCount++

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

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

And here is the .h file

#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(const FString& Guess);
	bool IsIsogram(const FString& Word) const;
	TArray<FString> Words;
	TArray<FString> GetValidWords(const TArray<FString>& WordList) const;
	void GetBullCows(const FString& Guess, int32& BullCount, int32& CowCount) const;


	// Your declarations go below!
	private:
	FString HiddenWord;
	int32 Lives;
	bool bGameOver;
	TArray<FString> Isograms;
};

This looks like it might be the problem. you are dividing 2 strings. I assume what you want is to take the paths and combine them.

you might want to add the / inside the quotes of the text and then use + instead to append the 2 strings.

If this doesn’t work, look at your crash report - usually it will have a line number for your code which should help - share it if you’re not sure.

It’s an operator overload for concatenating paths.

In the crash report it says:

UE4Editor_BullCowGame_5711!DispatchCheckVerify<void,<lambda_eca0ddd6835beb7598f8fd811950a276> >() [C:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Misc\AssertionMacros.h:165]
UE4Editor_BullCowGame_5711!UBullCowCartridge::SetupGame() [C:\C++\Bull Cow Game\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp:40]
UE4Editor_BullCowGame_5711!UBullCowCartridge::BeginPlay() [C:\C++\Bull Cow Game\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp:15]

And after looking it up, as far as i can see, the syntax in my code on line 12-13 in the .cpp file is the exact same as was used in the document in lecture 69 and it has worked so far until I introduced the code from lecture 80.

It looks as if you are setting the isograms array in BeginPlay before reading in from a file.

I see the validation code generating the array first so unless words is populated first, this may cause the issue you are seeing.

I concur with what Brian said. The assertion is coming from this line

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

And if you look at your BeginPlay function

// Words is empty here
Isograms = GetValidWords(Words);  // this is working with an empty Words
GetValidWords(Words); // P.S. this is redundant
// Populate Words here
const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");
FFileHelper::LoadFileToStringArray(Words, *WordListPath);

I managed to fix it with your great help, so I can continue the course now.

Thanks alot

2 Likes

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

Privacy & Terms