FPrintf in Unreal Engine with TEXT, FText, and FString

Introduction

TEXT is a macro for creating wide-character string literals, FString is a C++ string-like type for general text handling (e.g. logs), and FText is a specialized type for managing string translations and handling localized text for, among other, UI elements in Unreal Engine.

Not that FText objects are immutable (they cannot be modified), ensuring consistency during localization and preventing accidental text modifications.
In addition supports text variables, enabling dynamic adjustments to different languages and cultures for UI elements.

Code

// TEXT is a macro that converts a literal string into wide character ones.
// The result is platform-dependent; in my case, it's a `const wchar_t`.
const auto MyText = TEXT("Hello, ça va ?");

// FString is a C++ string-like type used for text manipulation. FString supports UTF-8 and can be used to log messages.
const FString MyString("Hello");

// FText is a data type primarily used for localization, especially useful for the UI.
const FText MyFText;

// const FText MyFText2(FString("Impossible")); // Private constructor, won't compile
const FText MyFText2 = FText::FromString("Hello");

// To dereference a FString as a pointer, useful for `%s`, you should use the `*` operator.
// FText has a `ToString()` function to convert it to FString.

// Note: The first format string (`Fmt` argument) must be of type TEXT. Otherwise, you will face a compilation error.
const FString String = FString::Printf(TEXT("TEXT:%s, FText: %s, String: %s"), MyText, *MyFText.ToString(), *MyString);

//
TEXT convert to a wchar_t*

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *