tl;dr How to construct an UEnum or creating a subclass of UEnum? … to enable compile time check for references to the UEnum
For enums, where I need a string connected to a value, I’m using
UENUM()
enum class EMyEnum : uint8
{
foo UMETA(DisplayName = "Foo"),
bar UMETA(DisplayName = "Bar"),
};
and retrieving DisplayName by using the static functions of class UEnum (which is bad style):
// Bad style: HeaderFileNameWhereTheEnumIsIn and EMyEnum are hardcoded and won't be checked at compile time.
auto myString = UEnum::GetDisplayValueAsText(TEXT("HeaderFileNameWhereTheEnumIsIn.EMyEnum"), EMyEnum::foo).ToString())
Futhermore, UEnum provides nice member functions for retrieving its amount of values etc. I’m aware of Ramas tutorial where he retrieves the UEnum object which was created by UE4 using the macro UENUM(). However, there the name of the Enum is also hardcoded as string, so no compile time check possible.
So I’d like to use my enums with an instance of UEnum where I don’t have to use FindObject. Or deriving from UEnum with my enums. How do I do that? What would be the downsides? [HR][/HR]
UEnum::UEnum(const FObjectInitializer& ObjectInitializer)
shouldn’t be used (flagged with macro DECLARE_CASTED_CLASS_INTRINSIC_NO_CTOR)
The internal construction of UEnum seems to be quite convoluted, e.g. from UObjectGlobals.cpp:
UEnum* NewEnum = new (EC_InternalUseOnlyConstructor, Outer, UTF8_TO_TCHAR(Params.NameUTF8), Params.ObjectFlags) UEnum(FObjectInitializer());