[SOLVED]BullCows game crashing after implementing the RandRange function

It compiles with no problems but then I hit play and it crashes. Been working on debugging this issue all day and can’t seem to find the reason. The line is exactly the same as what the video shows.

I’ve tried everything, even going back to using the .h file instead of populating the words at run time and it still crashes.

I even tried deleting the binaries and intermediate folder to see if that would solve the issue but I still get the crash.

Any ideas?

Line 40 is where the RandRange is implemented and line 18 is where SetUpGame is called

.cpp file

#include "BullCowCartridge.h"

#include "Misc/FileHelper.h"

#include "Misc/Paths.h"

// #inclide "Math/UnrealMathUtility.h" <- already included in #include "CoreMinimal.h"

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

    Super::BeginPlay();

    

    //Load words at runtime

    //Populate WordList

    const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");

    FFileHelper::LoadFileToStringArray(WordList, *WordListPath);

    //Get valid words from WordList

    Isograms = GetValidWords(WordList);

    SetupGame();

    

   // PrintLine(TEXT("The hidden word is %s"), *HiddenWord);

}

//main function that takes user input and checks against the HiddenWord

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

    if(bGameOver){

        ClearScreen();

        SetupGame();

    }else{

        ProcessGuess(Input);

       

    }

}

//Function that sets up the game parameters

void UBullCowCartridge::SetupGame(){

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

    Lives = HiddenWord.Len(); 

    bGameOver = false;

    PrintLine(TEXT("Welcome to Bull Cows!"));

    PrintLine(TEXT("Press [TAB] to enter the terminal...\n"));

    PrintLine(TEXT("You have %i tries.\nGuess the %i letter word..\nThen press [ENTER]"), HiddenWord.Len(), HiddenWord.Len());

}

//Function for ending the game

void UBullCowCartridge::EndGame(){

    bGameOver = true;

    PrintLine(TEXT("Press [ENTER] to play again..."));

}

//Function that does all the checks for the player's input

void UBullCowCartridge::ProcessGuess(const FString &Guess){

    PrintLine(TEXT("Your word:"));

    PrintLine(Guess);

    if(Guess == HiddenWord){

        PrintLine(TEXT("\nYou Win!!!\n"));        

        EndGame();

        return;

    //Check to make sure character length matches the HiddenWord length

    }else if(Guess.Len() != HiddenWord.Len()){

        PrintLine(TEXT("The hidden word is %i letters long\nTry again"), HiddenWord.Len());

        return;

    //Check the player's input is an isogram

    }else if(!bIsIsogram(Guess)){

        PrintLine(TEXT("No repeating letters. Guess again..."));

        return;

    //If guess is incorrect

    }else{

        --Lives;

        if(Lives != 0){

            PrintLine(TEXT("Sorry that is incorrect, try again.\nYou now have %i tries left"), Lives);

        }else{

            PrintLine(TEXT("Sorry, you are out of tries..."));

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

            EndGame();

        }

    }

}

//Function to check if player input is an isogram

bool UBullCowCartridge::bIsIsogram(const FString &Guess) const{

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

        for(int32 j = i + 1; j < Guess.Len(); j++){

            if(Guess[i] == Guess[j]){

                return false;

            }

        }

    }

    

    return true;

}

//Function to pass words from HiddenWordList.h to check if it's an isogram

TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString> &WordList) const{

    

    TArray<FString> ValidWords;

    //Ranged Based For-Loop is the best way to iterate over an entire collection

    for(FString WList : WordList){

        if(WList.Len() >= 4 && WList.Len() <= 8 && bIsIsogram(WList)){

          ValidWords.Emplace(WList); 

        }

    }

    return ValidWords;

}

.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 bIsIsogram(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;

    TArray<FString> WordList;

};

EDIT
Under further checking, I re-added some of the previous debug code from earlier videos and have noticed that my WordList isn’t populating. Not sure what I did wrong.

I figured the FFileHelper populates the WordList but am I supposed to include the .txt file anywhere?


2udemyBCIssues.PNG

EDIT
I finally figured out what I did. Ended up printing my WordListPath to the console because I was literally out of ideas on what could be done. Looking closer at the path I realized that I saved my .txt file into Sources when it should have been saved into the Content folder.
Everything is working beautifully now
Hopefully if someone else runs into this issue, this post of me talking to myself helps them.

1 Like

Nice safe-solve :slight_smile:

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

Privacy & Terms