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)
    TEnumAsByte<TreeType> Type;
  4. Add the following to the Tree constructor:
    auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cylinder.Cylinder'"));
    if (MeshAsset.Object != nullptr)
    {
      GetStaticMeshComponent()->SetStaticMesh(MeshAsset.Object);
      GetStaticMeshComponent()->bGenerateOverlapEvents = true;
    }
    GetStaticMeshComponent()->SetMobility(EComponentMobility::Movable);
  5. Create a new Blueprint class, called MyTree, based on Tree.
  6. Inside the blueprint editor for MyTree, click on the Construction Script tab.
  7. Right-click in the empty window, and type treetype. There is a Get number of entries in TreeType node.
    How to do it…
  8. Place it, and then connect its output pin to a Random Integer node.
    How to do it…
  9. Connect the output of the random integer to a ToByte node.
    How to do it…
  10. In the Variables section of the Blueprint panel, expand Tree and select Type.
    How to do it…
  11. Drag this into the graph, and select Set when you see a small context menu appear.
  12. Connect the output of the ToByte node to the input of the SET Type node. You'll see an extra conversion node automatically appear.
    How to do it…
  13. Lastly, connect the execution pin of Construction Script to the SET Type node's execution pin.
  14. Your Blueprint should look like the following:
    How to do it…
  15. To verify that the blueprint is correctly functioning and randomly assigning a type to our tree, we are going to add some nodes to the Event Graph.
  16. Place a Print String node after the Event BeginPlay event node.
    How to do it…
  17. Place a Format Text node, and connect its output to the input of the Print String node. A conversion node will be added for you.
    How to do it…
  18. Inside the Format Text node, add My Type is {0}! to the textbox.
    How to do it…
  19. Drag Type from the variables section of the Blueprint into the graph selecting Get from the menu.
    How to do it…
  20. Add an Enum to Name node to Type output pin.
    How to do it…
  21. Connect the Name output to the input pin on Format Text labelled 0.
    How to do it…
  22. Your Event Graph should now look like the following:
    How to do it…
  23. Drag a few copies of your Blueprint into the level and hit Play. You should see a number of trees printing information regarding their type, verifying that types are being randomly assigned by the Blueprint code that we created.
    How to do it…

How it works…

  1. As usual, we use StaticMeshActor as the base class for our Actor so that we can easily give it a visual representation in the level.
  2. Enumerated types are exposed to the reflection system using the UENUM macro.
  3. We mark the enum as Blueprint-available using the BlueprintType specifier.
  4. The enum declaration is just the same as we would use in any other context.
  5. Our Tree requires a TreeType. Because tree has tree-type is the relationship we want to embody, we include an instance of TreeType in our Tree class.
  6. As usual, we need to use UPROPERTY() to make the member variable accessible to the reflection system.
  7. We use the BlueprintReadWrite specifier to mark the property as having both get and set support within Blueprint.
  8. Enumerated types require being wrapped in the TEnumAsByte template when used in UPROPERTY, so we declare an instance of TEnumAsByte<TreeType> as the Tree's Type variable.
  9. The constructor changes for Tree are simply the standard load and initialize our static mesh component preamble used in other recipes.
  10. We create a Blueprint that inherits from our Tree class so that we can demonstrate the Blueprint-accessibility of the TreeType enum.
  11. In order to have the Blueprint assign a type to the tree at random when we create an instance, we need to use the Blueprint Construction Script.
  12. Within the Construction Script, we calculate the number of entries in the TreeType enum.
  13. We generate a random number, and use that as an index in the TreeType enum type to retrieve a value to store as our Type.
  14. The Random number node, however, returns integers. Enumerated types are treated as bytes in Blueprint, so we need to use a ToByte node, which can then be implicitly converted by Blueprint into an enum value.
  15. Now that we have Construction Script assigning a type to our tree instances as they are created, we need to display the tree's type at runtime.
  16. We do so with the graph attached to the BeginPlay event within the Event Graph tab.
  17. To display text on screen, we use a Print String node.
  18. To perform string substitution and print our type out as a human-readable string, we use the Format Text node.
  19. The Format Text node takes terms enclosed in curly braces, and allows you to substitute other values for those terms returning the final string.
  20. To substitute our Type into the Format Text node, we need to convert our variable stores from the enum value into the actual name of the value.
  21. We can do so by accessing our Type variable, then using the Enum to Name node.
  22. Name, or FNames in native code, are a type of variable that can be converted to strings by Blueprint, so we can connect our Name to the input on the Format Text node.
  23. When we hit play, the graph executes retrieving the type of tree instances placed in the level, and printing the names to the screen.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.214.32