TEXT(" ") macro to be considered when using LoadFileToArray() function

We have been taught to load up a big array of Words at runtime using LoadFileToArray() function but the Words file currently does not accommodate the TEXT("") macro and contain purely words.
It would be good to know how to write to file and adding TEXT macros to each words.

The TEXT macro is to ensure the correct data type for a character or string literal. That being

'a'
"string"

respectively.

Unreal’s native encoding is Unicode and different platforms handle that differently. For example Windows natively supports Unicode and uses the wchar_t type to store Unicode code points.

So instead of

'a'

Windows would need

L'a';

The L prefix makes the type of the literal wchar_t instead of char.

As other platforms require different character types to handle Unicode, Unreal provides TCHAR type alias and the TEXT macro so that the right type and literal is used for the targeted platform. On Windows TCHAR would be wchar_t and the TEXT macro prepends an L.

Thanks.
You have shown us how to fill an array from a text file during runtime, it would be really cool if you can show us how to write to file with words (isograms) from an array. Adding the TEXT macro syntax while doing it. Maybe with the help of materials from FileHelper.h .

The TEXT macro is for string and character literals only i.e. “things in quotes” in C++. If you have a variable that already holds a value, you don’t need to do anything.

i.e.

const FString Value = TEXT("Hello");
const FString ValueCopy = Value;

The above is correct. If you were to add the TEXT macro on the last line like

const FString ValueCopy = TEXT(Value);

This would be incorrect because as explained in my previous post it just prepends an L on Windows, meaning on Windows that would expand to be

const FString ValueCopy = LValue;

And LValue was never defined so that’s a compilation error.


FFileHelper has Save versions so iff you understand LoadFileToStringArray it should be pretty straightforward to understand.

const FString SavePath = FPaths::ProjectContentDir() / TEXT("WordList/Isograms.txt");
FFileHelper::SaveStringArrayToFile(Isograms, *SavePath);

Thanks, I will look into SaveStringArrayToFile.

To clarify : the file used to fill the array contains only words with no TEXT macro. I understand this works now but will fail when crossing platform. The subsequent lecture taught us multi cursor uses. Here we manually added TEXT macro to word array, using multi cursor. For sake of being consistent should we add Text macro when using LoadFileToStringArray?

Incorrect, the text file is not C++ code.

No, because it shouldn’t have them for the reasons I keep repeating.

Thanks. Confusion is stemming from text file to array. Assuming a file contains one string, assigning that string to an array via file and via hard code is not the same thing. But I will work on it.

It contains 0 strings as it is not code.

Thank you!

hi
i used the path function that was explained in section / class 69 my BeginPlay() function loads the txt file to member array and used RemoveAll() function to remove non-isogram words. Here is the BeginPlay()

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

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

Privacy & Terms