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
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
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
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!
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());
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 …
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);
I found this works for me in the code I’m running.
FText SocketNameStr = UEnum::GetDisplayValueAsText(SocketName);
FName AttachmentSocket(SocketNameStr.ToString());
const USkeletalMeshSocket *Socket = Char->GetMesh()->GetSocketByName(AttachmentSocket);
That works in shipped build?
@axelkomair01 UEnum::GetValueAsString()
works in shipped build, UEnum::GetDisplayValueAsText()
does not.
I just came across this thread today after finding out the hard way.
I’ve been using an enum with UMETA tag to represent mesh bone names which worked well in editor.
UENUM(BlueprintType)
enum class EBoneName : uint8
{
None UMETA(DisplayName = "none"),
Pelvis UMETA(DisplayName = "pelvis"),
RightHandSocket UMETA(DisplayName = "hand_r_socket"),
etc.
};
UMETA() will not be in a packaged build so the display name will return null.
I had to switch things around and use a different approach to get the names. The enums are now the bone/socket names with a function call to get the actual string. The string needs to be split since the output will be something like “EBoneName::hand_r_socket”, then convert to FName since that is commonly used for referencing bones etc.
UENUM(BlueprintType)
enum class EBoneName : uint8
{
None UMETA(DisplayName = "None"),
pelvis UMETA(DisplayName = "pelvis"),
hand_r_socket UMETA(DisplayName = "hand_r_socket")
etc.
};
const FString EnumString = UEnum::GetValueAsString(Enum);
FString LeftString;
FString RightString;
EnumString.Split(TEXT("::"), &LeftString, &RightString);
return FName(*RightString);