How can I output the value of an Enum to a log?

I’ve always been a big ENUM user inside UDK with uScript. I enjoy how they can keep things very clean if implemented right. However, I can’t seem to figure out how to actually use UE_LOG() to log their current value.

There appears to be no way (that I have found anyways) to convert the value of an ENUM into that of String, or even an Int which would give me a numerical value of its setting at the very least. I am trying to currently log my custom classes Role and RemoteRole (I understand that RemoteRole should be the opposite of Role (or at least in UDK)). But it won’t compile.

UE_LOG(InvLog, All, TEXT("Replication Properties: ROLE = '%s' ,REMOTEROLE = '%s') ") *Role, GetRemoteRole() );
1 Like

Well i did simple search and i find UEnum class

Which i assume stores all enum information for blueprint, there some static functions you might be interested in

My guess is you simply put template with enum type and in 2nd argument you value, but i don’t know how to get enum path :stuck_out_tongue: but you might figure out somehow

Definitly your enum declaration needs to be precursored with UENUM() so engine and UEnum class to see it.

1 Like

oh you faster ;p well i will leave my incomplete anwser as it got links to UEnum refrence

#There Is a Way

I wrote a Wiki tutorial on it here:

Original code was created with help of Marc Audy

FText is Localized

can use .ToString() to get it to FString if you want

FText GetVictoryEnumAsString(EVictoryEnum::Type EnumValue)
{
  const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EVictoryEnum"), true);
  if(!EnumPtr) return NSLOCTEXT("Invalid","Invalid","Invalid");
 
  return EnumPtr->GetDisplayNameText(EnumValue);
}

always nice to hear from you !

Yeah sorry, I don’t really understand this.

I’m assuming because Role located inside Actor.h is of type TEnumAsByte I can’t convert it back to a regular UEnum in order to use the GetDisplayName?

I’m not entirely sure what the point of TEnumAsByte is versus a regular UEnum in general, but it seems to have no other function than “GetValue()”.

Which is fine if it’s not possible at the moment, but a little annoying that any defined Epic ENUM that’s set to TEnumAsByte I can’t log for testing.

GetDisplayNameText function is only accessible when WITH_EDITOR is defined, so pay attention. I’ve corrected wiki page mentioned above.

If you’d like to do this without creating a function to convert the enum, here’s a simple way to use the method outlined above:

void AMyGameWeapon::SetWeaponState(EWeaponState::Type NewState)
{
     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));

The first line finds a pointer to the enum object from its namespace, and then in the UE_LOG call, we use that enum pointer to get a string value to output. Important to note is that the pointer isn’t guaranteed to be valid, so use a ternary operator to ensure that you safely handle a case where the pointer doesn’t exist.

This UE_LOG statement also supplies an example of grabbing a string from an enum that isn’t enclosed in a namespace and whose path is known.

1 Like

thanks a lot @sailentz, the unreal documentarion is really bad, finally found the ■■■■ preprocessor directive for editor

For me, this worked:

Enum Declared:

UENUM()
enum EGripState
{
	Open       UMETA(DisplayName = "Stopped"),
	CanGrip    UMETA(DisplayName = "CanGrip"),
	Gripping   UMETA(DisplayName = "Gripping"),
};

Property declared .h

UPROPERTY(BlueprintReadOnly)
	TEnumAsByte<EGripState> GripState;

Logged in .cpp

    UE_LOG(LogTemp, Warning, TEXT("GripState: %i"), GripState);

TEnumAsByte claims to “to store enumeration values as bytes in a type-safe way”:

4 Likes

For me @Shadowrivers answer steered me in the right direction. Just adding a little more detail for others who may need it. Using MyEnum as our enum.

UE_LOG(LogTemp, Log, TEXT("MyEnum is %s"), *UEnum::GetValueAsString(MyEnum));

Note: Since %s doesn’t accept a FString we have to prepend the *.

8 Likes

this is not working for me

Assuming your enum has the UENUM macro on the declaration, you can use

UEnum::GetDisplayValueAsText(value).ToString();

to get the declared UMETA(DisplayName = “MyEnumValue”) value as an FString.

1 Like
UE_LOG(LogTemp, Warning, TEXT("EnumState: %s"), *UEnum::GetValueAsString<EEnumState>(EnumVariable));

This is exactly what's posted for enums.
1 Like

UE_LOG(LogTemp, Warning, TEXT("EnumState: %s"), *UEnum::GetValueAsString(EnumVariable).ToString());

I had to add .ToString() for it to compile. Also, specifying the <EnumType> is not necessary.

It’s so interesting to see this forum topic evolve over the years :slight_smile:

LNK2019: unresolved external symbol class UEnum