Conversion of Enum to String

EGroundDirection value = EGroundDirection::DOWN;

const TCHAR* msg = ANSI_TO_TCHAR("EGroundDirection ");

const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, msg,true);

UE_LOG(LogTemp, Warning, TEXT("This is the data: %s"), *EnumPtr->GetEnumName(0));

UE_LOG(LogTemp, Warning, TEXT("This is the data: %s"), *EnumPtr->GetDisplayNameText(EGroundDirection ::DOWN).ToString());

In our project, I wanted to cut down on the boilerplate and add some developer-time assertions, so I rolled the solution about into a global function defined in a header like this:

/**
 * Convert the value of an enum to a string.
 *
 * @param EnumValue
 *	The enumerated type value to convert to a string.
 *
 * @return
 *	The key/name that corresponds to the value in the enumerated type.
 */
template<typename T>
FString EnumToString(const T EnumValue)
{
	FString Name = StaticEnum<T>()->GetNameStringByValue(static_cast<__underlying_type(T)>(EnumValue));

	check(Name.Len() != 0);

	return Name;
}

And then I can just call it anywhere with:

EnumToString(EnumTypeName::EnumOnTheList);

Note that I am using GetNameStringByValue(); I’m not sure why the code above used GetValueAsString().

4 Likes

Just in case it helps anyone, the following worked for me for class enum without any function definitions and with a variable that stored a particular enum:

const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EMyEnumClass"), true);
if (EnumPtr) {DisplayName = EnumPtr->GetDisplayNameTextByValue(static_cast<uint8>(MyEnumVariable));}

ANY_PACKAGE is deprecated in 5.1

After some testing, here is an alternate solution that is easier to use and doesn’t use the deprecated macro.

//////////////////////////////////////////////////////////////////////////
// Enum To String
// Usage Example:
//		FString EnumString = EnumToString( EnumValue );
//////////////////////////////////////////////////////////////////////////
template< typename T >
FString EnumToString( T EnumValue )
{
   static_assert( TIsUEnumClass< T >::Value, "'T' template parameter to EnumToString must be a valid UEnum" );
   return StaticEnum< T >()->GetNameStringByValue( ( int64 ) EnumValue );
}

Edit: I was erroneously using GetNameStringByIndex here, but I have corrected it here. This will work as stated in all cases now.

5 Likes

Otherwise you can use:

FString Name =  UEnum::GetValueAsString(EMyEnum::EnumValue);
10 Likes

FYI,

Fulezi’s solution and my solution above will both get the result, but there is one minor difference. Fulezi’s answer will return a fully scoped result, whereas my solution will drop the enum class scope.

For example:

FString Name1 = EnumToString< ENoiseAlgorithm >( ENoiseAlgorithm::FBM );

Name1 would be “FBM”

FString Name2 = UEnum::GetValueAsString( ENoiseAlgorithm::FBM );

Name2 would be “ENoiseAlgorithm::FBM”

Which one you should use really depends on what your needs are.

Edit: I have discovered a bug in my version. As it was originally written, my version of this function will require you to order your enum values from 0 upwards and by increments of 1. This was fine for most use cases, but should you need to order them with gaps in the values or out of order for some reason, then my original solution will give you the wrong result.

For example:

UENUM( BlueprintType )
enum class EMenuStackLocation : uint8
{
   None = 0xFF UMETA( Hidden ),

   BottomMost = 0,

   Default = 1,

   TopMost = 2,

   Count UMETA( Hidden ),
};

for this:

FString Name = UEnum::GetValueAsString( EMenuStackLocation::TopMost );

We get “EMenuStackLocation::TopMost”

But for this:

FString Name = EnumToString( ENekoUIMenuStackLocation::TopMost );

We get “Count”, which is incorrect.

If we reorder the None value to be at the end of the list after Count, then it will work fine (provided we don’t have value gaps between our enum constants, i.e. TopMost = 10 or something.)

My code can be fixed by changing the GetNameStringByIndex() to GetNameStringByValue(). I have corrected it in my post above as well.

5 Likes

This is the right way to do that,

Just
UEnum::GetValueAsString(YourEnum::EnumValue)

4 Likes

UEnum::GetValueAsString