How to do it...

  1. Create a new StaticMeshActor class called Tree using the editor.
  2. Insert the following code above the class declaration:
#pragma once

#include "CoreMinimal.h"
#include "Engine/StaticMeshActor.h"
#include "Tree.generated.h"

UENUM(BlueprintType)
enum TreeType
{
Tree_Poplar,
Tree_Spruce,
Tree_Eucalyptus,
Tree_Redwood
};

UCLASS()
class CHAPTER_09_API ATree : public AStaticMeshActor
{
  1. Add the following to the Tree class:
UCLASS()
class CHAPTER_09_API ATree : public AStaticMeshActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
ATree();

UPROPERTY(BlueprintReadWrite)
TEnumAsByte<TreeType> Type;
};
  1. Add the following to the Tree constructor:
#include "Tree.h"

#include "ConstructorHelpers.h"

// Sets default values
ATree::ATree()
{
// Set this actor to call Tick() every frame. You can turn
// this off to improve performance if you don't need it.

PrimaryActorTick.bCanEverTick = true;

auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>
(TEXT("StaticMesh'/Engine/BasicShapes/Cylinder.Cylinder'"));


UStaticMeshComponent * SM = GetStaticMeshComponent();

if (SM != nullptr)
{
if (MeshAsset.Object != nullptr)
{
SM->SetStaticMesh(MeshAsset.Object);
SM->SetGenerateOverlapEvents(true);
}
SM->SetMobility(EComponentMobility::Movable);
}
}

  1. Return to the Unreal Editor and compile your code.
  2. Create a new Blueprint class called MyTree, based on Tree, by right-clicking on the Tree object and selecting Create Blueprint class based on Tree. Once the menu comes up, click on the Create Blueprint Class button.
  3. Inside the blueprint editor for MyTree, click on the Construction Script tab.
  4. Right-click in the empty window and type treetype. There is a Get number of entries in TreeType node:

  1. Where and then connect its Return value output pin to the Max property of a new Random Integer node:

  1. Connect the Return Value output of the random integer to a ToByte (Integer) node:

  1. In the Variables section of the My Blueprint panel, click on the + button. From there, go to the Details tab and set the Variable Type to Tree Type. Afterward, set the Variable Name to RandomTree:

  1. Drag the RandomTree variable into the graph and select Set Random Tree when you see a small context menu appear.
  2. Connect the Return Value output of the ToByte node to the input of the SET Type node. You'll see an extra conversion node automatically appear.
  3. Lastly, connect the execution pin of Construction Script to the SET Type node's execution pin. Your Blueprint should look as follows:

  1. 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.
  2. Place a Print String node after the Event BeginPlay event node:

  1. 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:

  1. Inside the Format Text node, add My Type is {0}! to the Format text box:

You should see that it adds a new parameter, 0, which we can now set.

  1. Drag the RandomTree variable from the Variables section of the My Blueprint window into the graph and select Get from the menu:

  1. Add an Enum to Name node to the Type output pin:

  1. The Format Text node will not use a Name, so we will need to convert it into Text. Add a ToText (name) node to the Enum to Name output pin.
  2. Connect the Return Value output of the ToText (name) node to the 0 input pin on the Format Text node. Your Event Graph should now look as follows:

The completed Blueprint graph
  1. Compile your Blueprint and then return to the Unreal Editor.
  2. 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: