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() );
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 but you might figure out somehow
Definitly your enum declaration needs to be precursored with UENUM() so engine and UEnum class to see it.
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.