EnumToString Macro

From Epic Wiki
Jump to: navigation, search

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<UEnum>(ANY_PACKAGE, TEXT(etype), true) != nullptr) ? FindObject<UEnum>(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

Kris