WordList saved as a text file in Contents

hi,
as described in the section 69 : Loading Words At Runtime, i saved all the words in WordList.txt file and created folder under Contents.
Then created a variable TArrayWordList; in the BullCowCartridge.h file

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);	// Guess=Input
	bool IsIsogram(const FString& Guess) const;
	private:
		FString HiddenWord;
		int32 Lives{0};
		bool bGameOver{false};
		TArray<FString>WordList;
};

Then include neccessary header files in the BullCowCartridge.cpp file to load the WorldList.txt file and get the path as described

#include "Misc/FileHelper.h"
#include "Misc/Paths.h"

I also added another header file to use Random function so i can get a random hidden word from the list ;

#include "Math/UnrealMathUtility.h"

Then i did file path and loading txt file contents into WordList variable i created (TArray ) in the BeginPlay() function ;

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    const FString WordListhPath = FPaths::ProjectContentDir() / TEXT("WordList/WordList.txt");
    FFileHelper::LoadFileToStringArray(WordList, *WordListhPath);

    SetupGame();
}

I also revised the SetUp() function to set the HiddenWord from a word from WordList variable which reads the txt file and used a random function to make a random word selection everytime it plays;

void UBullCowCartridge::SetupGame()
{
    PrintLine(TEXT("Welcome to BULLCOW GAME"));

    HiddenWord = WordList [ FMath::RandHelper( WordList.Num() ) ];
    Lives = HiddenWord.Len();
    bGameOver = false;

    PrintLine(TEXT("Guess the %i letter hidden word"), HiddenWord.Len());
    PrintLine(TEXT("You have %i lives"), HiddenWord.Len());
    PrintLine(TEXT("Type your guess \nPress enter to continue..."));
}

After all this save everyting and switch unreal / project settings / packing and added WordList folder as a non-asset directories to package as described ;

Everything worked , compiled and played nicely :slight_smile:

3 Likes

i revised the BeginPlay() function to remove the non-isogram words from WordList array.
Originally it was loading WordList.txt and converted to a string array and store it in member variable.
After that i used RemoveAll() function to removed by passing a predicate ; a lambda that capturesthis and gets a variable (word) and return if it is Isogram() or not using our member function,. then RemoveAll function removed all matcing words from the Array. Result ing number is displayed just for reference;

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    const FString WordListhPath=FPaths::ProjectContentDir() / TEXT("WordList/WordList.txt");
    FFileHelper::LoadFileToStringArray(WordList, *WordListhPath);
    WordList.RemoveAll([this](auto& word) {return IsIsogram(word); });
    SetupGame();
    PrintLine(TEXT("Hidden Word List has %i"), WordList.Num());  
}
1 Like

Privacy & Terms