This is a short article on how to convert an enumerator
to FString
in Unreal Engine.
Blueprints
In Blueprints, it is easy to convert all supported enumerators
to FString
. Simply drag your enumerator to the required pin, which will automatically add the necessary conversion node.
C++
In C++, the implementation varies based on the type of enum used.
Unreal Reflection System
If you want to use a solution that is compatible with the Unreal Reflection System and accordingly uses UENUM
, there is an in-built method named UEnum::GetValueAsString
.
Header file: Source file:Unreal Reflection System-friendly solution
// This is your enum
UENUM()
enum class EnumExample : uint8
{
EnumValue1,
EnumValue2
};
// This is how you obtain the string representation of the enumerator
FString StringRepresentation = UEnum::GetValueAsString(EnumExample::EnumValue2);
Pure C++
If you want to use a pure C++ solution, you should create a function to manually convert the enumerator.
Header file: Source file:Pure C++ solution
// This is your enum
enum class EnumExample : uint8
{
EnumValue1,
EnumValue2
};
class AnyClass
{
public:
static constexpr FString EnumToString(EnumExample Enum);
};
constexpr FString AnyClass::EnumToString(EnumExample Enum)
{
switch (Enum)
{
case EnumExample::EnumValue1:
return TEXT("EnumValue1");
case EnumExample::EnumValue2:
return TEXT("EnumValue2");
default:
return TEXT("Invalid");
}
}
// This is how you obtain the string representation of the enumerator
FString StringRepresentation = EnumToString(EnumExample::EnumValue2);