It is one of the poorer practices that is filling the tutorial space of unreal engine c++. Enumerators (enums) are very useful for defining multiple types of a thing such as:
Defining an enum like these leaves an issue when using them in TMaps or TArrays, the compiler will begin to tell you to enclose the type declaration in ‘TEnumAsByte’. Now while this will solve the error and bring that sweet relief of a green launch command. It would be better to look at what this is doing under the hood.
Where a Boolean sits as either a 0 (false) or 1 (true), think of an enumerator as a longer Boolean where we can have from 0 to UE_BIG_NUMBER, where each value corresponds with a name you’ve given:
So, using TEnumAsByte is simple do that conversion in the moment. But what are the drawbacks?
Which is better?
Define your enums using enum class EYourEpicType : uint8!
Using the ‘class’ specifer encloses the names of your enum to the scope of itself (like any other class). So our fruit combo, best practice, would look like this:
This removes the need to use TEnumAsByte, translates seamlessly to blueprint with zero hassle, and allows for duplicate enum names enclosed in each scope! Happy coding.
Swap TEnumAsByte to just your EAwesomeEnum by declaring your enum types using the ‘class’ specifier like so: