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.
Enumerated types are exposed to the reflection system using the UENUM macro.
We mark the enum as Blueprint-available using the BlueprintType specifier.
The enum declaration is just the same as we would use in any other context.
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.
As usual, we need to use UPROPERTY() to make the member variable accessible to the reflection system.
We use the BlueprintReadWrite specifier to mark the property as having both get and set support within Blueprint.
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.
The constructor changes for Tree are simply the standard load and initialize our static mesh component preamble that's used in other recipes.
We create a Blueprint that inherits from our Tree class so that we can demonstrate the Blueprint-accessibility of the TreeType enum.
To have the Blueprint assign a type to the tree at random when we create an instance, we need to use the Construction Script Blueprint.
Within the Construction Script, we calculate the number of entries in the TreeType enum.
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.
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.
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.
We do so with the graph attached to the BeginPlay event within the Event Graph tab.
To display text on screen, we use a Print String node.
To perform string substitution and print our type out as a human-readable string, we use the Format Text node.
The Format Text node takes terms enclosed in curly braces and allows you to substitute other values for those terms by returning the final string.
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.
We can do so by accessing our Type variable and then using the Enum to Name node.
Names, or FNames in native code, are a type of variable that can be converted into strings by Blueprint so that we can connect our Name to the input on the Format Text node.
When we hit Play, the graph executes, retrieving the type of tree instances that have been placed in the level and printing the names to the screen.