Download

UEnum and GetValueAsString

Hi all,

I need to get my enum value as string but I didn’t understand the first param : “Full enum path”.

If anyone can tell me please ?

Thanks :slight_smile:

For converting enums, we use the following function:


const FString EnumToString(const TCHAR* Enum, int32 EnumValue)
{
    const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, Enum, true);
    if (!EnumPtr)
        return NSLOCTEXT("Invalid", "Invalid", "Invalid").ToString();

#if WITH_EDITOR
    return EnumPtr->GetDisplayNameText(EnumValue).ToString();
#else
    return EnumPtr->GetEnumName(EnumValue);
#endif
}

We use it like so:


ETeam Team = ETeam::Alpha;
FString message = TEXT("Our enum value: ") + EnumToString(TEXT("ETeam"), static_cast<uint8>(Team));
// This prints: "Our enum value: ETeam::Alpha"

Ok, I will try this thank for answer :smiley:

EDIT : Works !

Came through this and I wanted to post an even simpler solution that I found:
Use **UEnum::GetValueAsString<EnumType>(const EnumType EnumeratorValue). **Insert your enum as a TEnumAsByte. Here’s an example:



// Get an enum from wherever you get it in your code, and set it into a member, in this example that is SurfaceType.

const TEnumAsByte<EPhysicalSurface> SurfaceEnum = SurfaceType;
FString EnumAsString = UEnum::GetValueAsString(SurfaceEnum.GetValue());



This two lines will already work.
Cheers!

2 Likes

Since this has costs me some time and I am still confused why GetValueAsString is giving back not the value only.
What i ended up is doing a .RightChop by the name +2 of the enum to get the Value only.


 FString FileEnding = UEnum::GetValueAsString<EDesiredImageFormat>(WriteOptions.Format).RightChop(21); 

isnt there a easier and dynamic soltuion to only get the value as sting not the whole Enum::Value ?

More shortly:
UE4.24.3


FString LocalRoleEnumString = UEnum::GetValueAsString<ENetRole>(GetLocalRole());


FString LocalRoleEnumString = UEnum::GetValueAsString(GetLocalRole());

2 Likes

I’m using 4.21.2.
I was having problem with FindObject, so I resorted to this:



UEnumProperty* EnumPtr = FindFieldChecked<UEnumProperty>(UUrathaAbility::StaticClass(), FName("TalentTree"));
if (EnumPtr)
{
   UE_LOG(LogTemp, Warning, TEXT("Enum name: %s"), *EnumPtr->GetName());
   if (EnumPtr->GetEnum())
   {
       treeName = EnumPtr->GetEnum()->GetDisplayNameText(static_cast<uint8>(TalentTree));
       UE_LOG(LogTemp, Warning, TEXT("TalentTree name: %s"), *treeName.ToString());
    }
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("Ptr not found..."));
}


treeName contains the Display name of the Enum after this code is executed. E.g. treeName → “General”


UENUM(BlueprintType)
enum class EUrathaAbilityTalentTree : uint8
{
    General = 0 UMETA(DisplayName = "General"),
    etc...
};

This is not working for me, i Still get EEnumName::Value
Taking the Display Name is not a super good solution …

This is not working for me, i Still get EEnumName::Value
Taking the Display Name is not a super good solution …


UEnum::GetDisplayValueAsText(Value).ToString()

You might like the approach we outlined on our blog, using compiler macros and templates.
Usage would look like this:


EWlanNotificationMSM Notification = EWlanNotificationMSM::Unknown;

UE_LOG(LogTemp, Warning
, TEXT("Received MSM notification: %s")
, *BIQ::Util::EnumValueStringify(Notification).ToString()
);

Been using:



static const FString EnumToString(const TCHAR* Enum, int32 EnumValue);

const FString UAWHelper::EnumToString(const TCHAR* Enum, int32 EnumValue)
{ const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, Enum, true);
  if (!EnumPtr)
return NSLOCTEXT("Enum not found", "Enum not found", "Enum not found").ToString();

return EnumPtr->GetNameStringByIndex((int32)EnumValue);
 }

FString NPCName = AWEnumToString(TEXT("ENPCClassEnum"), ename);