hi below is my :GetValidWords() function ; it return void and loads the words from txt file directly filtering IsIsogram and length criteria using lambda…
I think there is no reason to make a copy & return TArray from GetValidWords() because it already loads the words into a member variable which i called WordList (which is Isograms in the class).
I used LoadFileToStringArrayWithPredicate() function which reads the text file and loads it to member variable Wordlist and uses a predicate to select the words. This is UE4.25 version.
My version of lambda is a little different than show in the next section. It captures “this” to be able to use IsIsogram() member function.
auto result=FFileHelper::LoadFileToStringArrayWithPredicate(WordList, *WordListhPath,
[this](const auto& word) {return IsIsogram(word) && word.Len() <= 8 && word.Len() >= 4; });
The result is a bool and i also used UE_LOG() if the file cannot be read for debugging purposes.
My code also used FStringView while passing the Input into functions to avoid copying. This is a 4.25 feature as well. So i tried to optimize as much as i can;
void UBullCowCartridge::GetValidWords()
{
const FString WordListhPath = FPaths::ProjectContentDir() / TEXT("WordList/WordList.txt");
//FFileHelper::LoadFileToStringArray(WordList, *WordListhPath);
auto result=FFileHelper::LoadFileToStringArrayWithPredicate(WordList, *WordListhPath, [this](const auto& word) {return IsIsogram(word) && word.Len() <= 8 && word.Len() >= 4; });
// Error message if the file is not loaded
if(!result) UE_LOG(LogTemp, Warning, TEXT("File Not Found %s"), *WordListhPath);
}
Here is my BullCowCartridge.h file for reference ;
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
//virtual void OnInput(const FString& Input) override;
virtual void OnInput(FStringView Input) override;
void SetupGame();
void EndGame();
void ProcessGuess(FStringView Guess); // Guess=Input
void GetValidWords();
bool IsIsogram(FStringView Guess) const;
private:
FString HiddenWord;
int32 Lives{0};
bool bGameOver{false};
TArray<FString>WordList;
};