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

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