Last active
September 23, 2024 12:16
-
-
Save trentpolack/ad361a4d3d6e46d1a1ff82c03a997f28 to your computer and use it in GitHub Desktop.
Enumerations in Unreal Engine 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generally, developers in UE4 tend towards the following convention for enumerations: | |
UENUM( BlueprintType ) | |
enum class ENoiseGeneratorCellularType : uint8 | |
{ | |
NGCT_Natural UMETA( DisplayName = "Natural" ), | |
NGCT_Euclidean UMETA( DisplayName = "Euclidean" ), | |
NGCT_Manhattan UMETA( DisplayName = "Manhattan" ), | |
NGCT_Max UMETA( Hidden ) | |
}; | |
// This works well enough, but it does require care in how you name enumeration values. | |
// Additionally, if you are serializing enumerations values into a proprietary file format or type. | |
// For instance: I use text-based, non-binary JSON files for instance) so the numeric value of an enumeration types is necessary. | |
// | |
// To work around that, I tried this: | |
UENUM( BlueprintType ) | |
enum ENoiseGeneratorCellularType | |
{ | |
NGCT_Natural UMETA( DisplayName = "Natural" ), | |
NGCT_Euclidean UMETA( DisplayName = "Euclidean" ), | |
NGCT_Manhattan UMETA( DisplayName = "Manhattan" ), | |
NGCT_Max UMETA( Hidden ) | |
}; | |
// This will throw an error in any class/structure members that are specified as ENoiseGeneratorCellularType. Replace that with: | |
// NOTE: Methods and local variables do not necessarily need to use this template. | |
TEnumAsByte< ENoiseGeneratorCellularType > CellularType; | |
// However, there is an even nicer way that still retains the standard enumeration support (to my knowledge) and allows you to use C#-styled enumeration values: | |
UENUM( BlueprintType ) | |
namespace ENoiseGeneratorCellularType | |
{ | |
enum Type | |
{ | |
Natural UMETA( DisplayName = "Natural" ), | |
Euclidean UMETA( DisplayName = "Euclidean" ), | |
Manhattan UMETA( DisplayName = "Manhattan" ), | |
Max UMETA( Hidden ) | |
}; | |
} | |
// Changing TEnumAsBute to: | |
TEnumAsByte< ENoiseGeneratorCellularType::Type > CellularType; | |
// From there on out, you can access enumeration values like this (without any global name conflicts): | |
CellularType = ENoiseGeneratorCellularType::Natural; | |
// Yay magic. |
The latter example seems like it should still be valid, but I don't have a proper dev environment anymore — this was also a setup from a couple years ago, so it may be invalid now.
Same Here I got a lot of error when i try to create Enumerations
With 4.26 I used this one and it seems to work quite well.
UENUM( BlueprintType )
enum ENoiseGeneratorCellularType
{
NGCT_Natural UMETA( DisplayName = "Natural" ),
NGCT_Euclidean UMETA( DisplayName = "Euclidean" ),
NGCT_Manhattan UMETA( DisplayName = "Manhattan" ),
NGCT_Max UMETA( Hidden )
};
...
TEnumAsByte< ENoiseGeneratorCellularType > CellularType;
If you use enum ENoiseGeneratorCellularType : uint8
the build will fail with error : Missing '}' in 'Enum'
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I tried having multiple enums in a namespace, as in your example 3, but the UnrealBuildHeaderTool comes out with an error of error : Missing '}' in 'Enum' and not sure why.