Creating C++ enums that can be used in Blueprint

Enums are commonly used in C++ as flags or inputs to switch statements. However, what if you want to pass an enum value to or from C++ from a Blueprint? Alternatively, if you want to use a switch statement in Blueprint that uses an enum from C++, how do you let the Blueprint editor know that your enum should be accessible within the editor? This recipe shows you how to make enums visible in Blueprint.

How to do it…

  1. Create a new StaticMeshActor class called Tree using the editor.
  2. Insert the following code above the class declaration:
    UENUM(BlueprintType)
    enum TreeType
    {
      Tree_Poplar,
      Tree_Spruce,
      Tree_Eucalyptus,
      Tree_Redwood
    };
  3. Add the following UPROPERTY in the Tree class:
    UPROPERTY(BlueprintReadWrite) ...

Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.