I’m trying to understand how to create Enums that I can use in Unreal with C++. I’ve seen documentation that says the correct way to build them is like this:
#include "GroundDirection.generated.h"
UENUM(BlueprintType)
enum GroundDirection
{
DOWN UMETA(DisplayName = "DOWN"),
LEFT UMETA(DisplayName = "LEFT"),
UP UMETA(DisplayName = "UP"),
RIGHT UMETA(DisplayName = "RIGHT")
};
This doesn’t seem right however because I am getting errors in Visual Studio. Specifically it says:
Cannot open source file GroundDirection.generated.h"
So I have a few questions:
-
What am I doing wrong to make it error?
-
How do I add a number value to me enum values? In Java or C# I could do something like this:
public enum GroundDirection {
DOWN = 0, LEFT = 1 , UP = 2, RIGHT = 3
}
3 Likes
If you getting that error only in Error List then just ignore it, that file needs to be generated first so after compile this should disappear… even if not and if still works it’s ok. You should only consider errors during compilation as IntelliSense have problem understanding tricks used in UE4 and create false errors.
As for value contain enums in C++ it a feature of co called enum classes
https://en.cppreference.com/w/cpp/language/enum
UE4 reflection system only supports uint8 enums, so you can do this:
UENUM(BlueprintType)
enum class GroundDirection : uint8 {
DOWN = 0 UMETA(DisplayName = "DOWN"),
LEFT = 1 UMETA(DisplayName = "LEFT"),
UP = 2 UMETA(DisplayName = "UP"),
RIGHT = 3
}
You don’t need to type i values either, compiler will automatically use numbers from 0
14 Likes
Note that Shadowriver’s example is not exactly correct. UENUMs should be prefixed with “E“, and it is missing a semicolon at the end. Here’s a complete example:
UENUM(BlueprintType)
enum class EGroundDirection : uint8
{
Down = 0 UMETA(DisplayName = “Down”),
Left = 1 UMETA(DisplayName = “Left”),
Up = 2 UMETA(DisplayName = “Up”),
Right = 3 UMETA(DisplayName = “Right”)
};
1 Like