- Create a new StaticMeshActor class called Tree using the editor.
- 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
{
- 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;
};
- 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);
}
}
- Return to the Unreal Editor and compile your code.
- 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.
- Inside the blueprint editor for MyTree, click on the Construction Script tab.
- Right-click in the empty window and type treetype. There is a Get number of entries in TreeType node:
- Where and then connect its Return value output pin to the Max property of a new Random Integer node:
- Connect the Return Value output of the random integer to a ToByte (Integer) node:
- 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:
- Drag the RandomTree variable into the graph and select Set Random Tree when you see a small context menu appear.
- 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.
- Lastly, connect the execution pin of Construction Script to the SET Type node's execution pin. Your Blueprint should look as follows:
- 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.
- Place a Print String node after the Event BeginPlay event node:
- 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:
- 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.
- Drag the RandomTree variable from the Variables section of the My Blueprint window into the graph and select Get from the menu:
- Add an Enum to Name node to the Type output pin:
- 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.
- 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
- Compile your Blueprint and then return to the Unreal Editor.
- 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: