In this lesson, you will learn how to create custom UENUM types to replace magic numbers with human-readable labels, making your game logic safer and easier to manage in the Editor.
Why Use Enumerations?
When building gameplay systems—like a character's current stance (Idle, Crouched, Prone)—using raw integers (0, 1, 2) is error-prone. If you accidentally pass a 5 where a 2 is expected, your game might crash or behave strangely. Unreal’s UENUM macro allows you to define a restricted set of named constants that the engine understands, providing type safety and a clean dropdown menu in the Unreal Editor.
Defining Your Enum
To define an enumeration, use the UENUM macro before your standard C++ enum class. We use BlueprintType so the engine can expose this to your designers.
UENUM(BlueprintType)
enum class ECharacterStance : uint8
{
Idle,
Crouched,
Prone
};
By specifying : uint8 at the end, you ensure the enumeration consumes only one byte of memory, which is highly efficient for network replication and storage.
Using the Enum in Your Classes
Once defined, you can use your new type inside a UPROPERTY. This automatically generates a dropdown menu in the Details Panel, preventing users from entering invalid values.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Gameplay")
ECharacterStance CurrentStance;
Logic Control with Switch Statements
Because UENUM acts as a strongly-typed variable, you can use standard C++ switch statements to control game behavior. This is much cleaner than long if/else chains:
switch (CurrentStance)
{
case ECharacterStance::Idle:
// Handle idle logic
break;
case ECharacterStance::Crouched:
// Handle crouched logic
break;
case ECharacterStance::Prone:
// Handle prone logic
break;
}
By leveraging UENUM, you transform vague data into descriptive, self-documenting code. Not only does this prevent bugs, but it also empowers designers to modify game state through the Editor without needing to touch your underlying C++ files.
Key Takeaway: Use UENUM(BlueprintType) to replace magic numbers with descriptive, type-safe labels that provide clean, selectable options in the Unreal Editor.