TEXT("Fire") vs "Fire"

Tutorial uses:
PlayerInputComponent->BindAction(TEXT(“Fire”), IE_Pressed, this, &ATank::Fire);

I’ve used:
PlayerInputComponent->BindAction(“Fire”, IE_Pressed, this, &ATank::Fire);

is there a technical reason to used TEXT prefix because the results are the same?

BindAction takes a FName type for the first parameter, and FName itself can be constructed with different types of strings, mainly ANSI 8-bit character (char*) strings or 16-bit wide character (wchar_t*) strings. The compiler will convert your string literal to whatever character type is defined for the project (typically wide characters are used for projects that are localized). This is why you don’t need the TEXT macro for BindAction.

However, you might try to call a function that takes a parameter of a specific character type, such as FString::Printf. Depending on the platform, this could be a regular character string, or a wide character string, so how do you know which one to pass? This is where the TEXT macro comes in. The macro will automatically convert your string literal to the correct type before the compiler runs (i.e. during the preprocessor step).

2 Likes

Just to note that the size of wchar_t is dependant on platform. On Linux they are 32-bits/4 bytes.

I recall seeing an Epic developer saying FName doesn’t support Unicode and has to perform a conversion if given wchar_t. Not sure if that’s changed at all since then (it was a few years ago that).

It might also be worth noting that the FName object itself just stores 3 32-bit ints.

Yep, it varies by platform. I’ve always been a Windows guy so I have a tendency to stick to “how it works on Windows” in my explanations unless the question is specifically about the difference in platforms. Old habits I suppose. :slight_smile:

If you look at NameTypes.h, it looks like support for constructing an FName with a UTF-8 literal was added in UE5.

For the sake of the OP’s question about the TEXT macro, another way to think of it is that the string will either be converted to: L"My String" or u8"My String" or u"My String", depending on platform.

If you’re interested in venturing further down the rabbit hole of character encodings, this page has some good info.

2 Likes

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

Privacy & Terms