TNameOf does not work for UEnums?

I’m trying to retrieve the name of an enum type in a template.

I found TNameOf<T> which seems to be meant to achieve exactly this for seemingly all types known to the unreal reflection system, but it simply throws an assertion instead. What am I doing wrong here?

Sample code:

UENUM(BlueprintType)
enum class EMyUEnumType : uint8
{
    One,
    Two,
    Three
};

FName name = TNameOf<EMyUEnumType>::GetName(); // Hits an assertion ("crashes")

I resorted to using the following workaround until UHT is adjusted to generate correct specializations for TNameOf:

// Once in a common header
#define Workaround_Expose_TNameOf(type) \
template<> \
struct TNameOf<type> \
{  \
	FORCEINLINE static TCHAR const* GetName() \
	{ \
		return TEXT(#type); \
	} \
}

// After every UEnum (replace EMyUEnumType by the enum's typename)
Workaround_Expose_TNameOf(EMyUEnumType);

Hey!

Ive linked this before on another answer…
Rama did a good explanation about enums… if you wanna get enum name which is reflected…
Maybe this is the proper way or atleast a good starting point :wink:

Can you please provide an example based on that explanation by Rama on how to achieve what I am asking for? As it stands this right now this is not even an attempt to answer my question :confused:

I provided code samples of what I am looking for. Please use them :slight_smile:

Hey Sure

UENUM(BlueprintType)
enum class EMyUEnumType : uint8
{
     One    UMETA(DisplayName="One"),
     Two    UMETA(DisplayName="TWO"),
     Three UMETA(DisplayName="Three")

 };

EMyUEnumType EnumValue;

FString EnumString = GetEnumValueAsString<EMyUEnumType >("EMyUEnumType ", EnumValue)));

Looks like you didn’t understand my question. I’m looking for a way to retrieve “EMyUEnumType” from the type (and without macros) instead of having to pass it as a parameter myself.

well so you wanna return enum without know what enum is that?
thats impossible i thinkbecause compiler need to know what he need return unless you return UENUM…

You can return enum from string but you need to know exactly what enum type you want back or you can get any enum as byte.

but you can try this:

      const UEnum* WeaponStateEnum = FindObject<UEnum>(ANY_PACKAGE, TEXT("EWeaponState"));
      UE_LOG(LogMyGameWeapon, Log, TEXT("SetWeaponState called on weapon %s with state %s (Role %s)")
             , *GetNameSafe(this)
             , *(WeaponStateEnum ? WeaponStateEnum->GetEnumName(NewState) : TEXT("<Invalid Enum>"))
             , *UEnum::GetValueAsString(TEXT("Engine.ENetRole"), Role));

source: Conversion of Enum to String - C++ - Unreal Engine Forums

This will return name as string without knowing the real enum, but not the enum type…

Thank you for your suggestion, but once I have the stringified typename I’m already done with regard to this question. I’m looking for a way to ask the Unreal reflection system for the stringified name of a given actual type.

In other words: I do have

EMyUEnumType

and I want

FName(TEXT("EMyUEnumType"))

And I want to achieve this without macros on my side or entering the string myself. TNameOf looks like a template meant to achieve just that, but it seems not to work as I expected in current engine versions.