EnumToString Macro

Overview Macro that lets you quickly get an enum value as a FString for use in logging etc. Caveat - only works with enumerators defined using UENUM() macro. YourProject.h #define GETENUMSTRING(ety...

Updated about 4 years ago Edit Page Revisions

Overview

Macro that lets you quickly get an enum value as a FString for use in logging etc.
Caveat - only works with enumerators defined using UENUM() macro.

YourProject.h

#define GETENUMSTRING(etype, evalue) ( (FindObject(ANY_PACKAGE, TEXT(etype), true) != nullptr) ? FindObject(ANY_PACKAGE, TEXT(etype), true)->GetEnumName((int32)evalue) : FString("Invalid - are you sure enum uses UENUM() macro?") )

Usage

YourClass.h

UENUM()
enum class EUsesEnum : uint8
{
    UseEnum_Bad,
    UseEnum_Good,
    UseEnum_Nice,
    UseEnum_Naughty
};

YourClass.cpp

void AYourClass::SomeFunction()
{
    EUsesEnum UseEnumValue = EUsesEnum::UseEnum_Good;
    UE_LOG(LogSomething, Log, TEXT("UseEnumValue as string : %s"), *GETENUMSTRING("EUsesEnum", UseEnumValue));
}

Output:
LogYourCategory: UseEnumValue as string : UseEnum_Good

Credits