Creating Enums in C++

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:

  1. What am I doing wrong to make it error?

  2. 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
    }

2 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

6 Likes