Hi.
I am stuck on lesson 78 (Out of the frying pan) of the BullCow game in the Unreal Engine C++ course.
As far as I can tell, I have narrowed the problem down to my TArray named Isograms. I declare it and fill it in the BeginPlay method, but when I try to calculate its size in the SetupGame method using .Num it goes from having over 430 words in the TArray to nothing being in it, causing the .Num() to crash Unreal.
Please find attached my code (header file and C++ file) below. Any help would be greatly appreciated (Apologies for the strange formatting. I’m not sure why that is happening).
#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> GetValidWords(const TArray<FString>& Words) const;
// Your declarations go below!
private:
TArray<FString> WordList;
FString HiddenWord;
int32 Lives;
bool bGameOver;
TArray<FString> Isograms;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
//#include "HiddenWordList.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");
FFileHelper::LoadFileToStringArray(WordList, *WordListPath);
//TArray<FString> ValidWords = GetValidWords(WordList);
TArray<FString> Isograms = GetValidWords(WordList);
PrintLine(TEXT("The number of Isograms is %i"), Isograms.Num());
PrintLine(TEXT("ValidWords - 1 is %i"), Isograms.Num() -1 );
PrintLine(TEXT("DEV TEST: %s"), *Isograms[0]);
SetupGame();
//PrintLine(TEXT("%i"), FMath::RandRange(0,10));
// PrintLine(TEXT("The number of possible words is %i"), WordList.Num());
// PrintLine(TEXT("The number of valid words is %i"), ValidWords.Num());
// PrintLine(TEXT("ValidWords - 1 is %i"), ValidWords.Num() -1 );
}
void UBullCowCartridge::OnInput(const FString &Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
//HiddenWord = GetValidWords(WordList)[FMath::RandRange(0, GetValidWords(WordList).Num()-1)];
HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num()-1)];
PrintLine(TEXT("%i"), Isograms.Num());
//HiddenWord = "Cake";
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Welcome to Bull Cow!"));
PrintLine(FString::Printf(TEXT("Guess the %i letter word!"), HiddenWord.Len())); //prompt player for a guess.
PrintLine(TEXT("The hidden word is: %s."), *HiddenWord); // Debug Line
}
void UBullCowCartridge::ProcessGuess(const FString &Guess)
{
if (Guess == HiddenWord)
{
ClearScreen();
PrintLine(TEXT("You have won!"));
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len())
{
ClearScreen();
--Lives;
PrintLine(TEXT("Incorrect guess.\nThe word has %i characters."), HiddenWord.Len(), Lives);
PrintLine(TEXT("Lives: %i"), Lives);
return;
}
//Check if Isogram
if (!IsIsogram(Guess))
{
PrintLine(TEXT("Not an Isogram. \nGuess again!"));
return;
}
--Lives;
if (Lives <= 0)
{
ClearScreen();
PrintLine(FString::Printf(TEXT("You have no lives left")));
PrintLine(FString::Printf(TEXT("The hidden word was %s"), *HiddenWord));
EndGame();
return;
}
PrintLine(TEXT("Incorrect guess.\nYou have lost a life!"));
PrintLine(TEXT("Lives: %i"), Lives);
PrintLine(FString::Printf(TEXT("The hidden word was %s"), *HiddenWord));
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("Press enter to continue."));
}
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])
{
//PrintLine(TEXT("Not an Isogram!.\n"));
return false;
}
}
}
return true;
}
TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString> &Words) const
{
TArray<FString> ValidWords;
for (FString Word : Words)
{
if (Word.Len() >= 4 && Word.Len() <= 8 && IsIsogram(Word))
{
ValidWords.Emplace(Word);
}
}
return ValidWords;
}