How to do it...

  1. Create a new StaticMeshActor class called King. Add the following to the class header:
#pragma once

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


DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnKingDeathSignature, AKing*, DeadKing);
UCLASS()
class CHAPTER_09_API AKing : public AStaticMeshActor
{
GENERATED_BODY()
  1. We also want to display something on the screen, so add a definition for a constructor:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnKingDeathSignature, AKing*, DeadKing);
UCLASS()
class CHAPTER_09_API AKing : public AStaticMeshActor
{
GENERATED_BODY()

// Sets default values for this actor's properties
AKing();
};
  1. Add a new UFUNCTION to the class:
UFUNCTION(BlueprintCallable, Category = King) 
void Die(); 
  1. Add an instance of our multicast delegate to the class as well:
UPROPERTY(BlueprintAssignable) 
FOnKingDeathSignature OnKingDeath; 
  1. Open the King.cpp file and then add in the implementation for the constructor to perform our mesh initialization (remembering to add an #include for the ConstructionHelpers.h file):
#include "King.h"
#include "ConstructorHelpers.h"

// Sets default values
AKing::AKing()
{
// 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/Cone.Cone'"));


UStaticMeshComponent * SM = GetStaticMeshComponent();

if (SM != nullptr)
{
if (MeshAsset.Object != nullptr)
{
SM->SetStaticMesh(MeshAsset.Object);
SM->SetGenerateOverlapEvents(true);
}
SM->SetMobility(EComponentMobility::Movable);
}
}
  1. Implement the Die function:
void AKing :: Die () 
{ 
  OnKingDeath.Broadcast(this); 
} 
  1. Create a new class called Peasant, also based on StaticMeshActor.
  2. Declare a default constructor in the class:
APeasant (); 
  1. Declare the following function:
UFUNCTION(BlueprintCallable, category = Peasant) 
void Flee (AKing * DeadKing); 
  1. Implement the constructor:
#include "Peasant.h"
#include "ConstructorHelpers.h"

APeasant::APeasant()
{
// 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/Cube.Cube'"));

UStaticMeshComponent * SM = GetStaticMeshComponent();

if (SM != nullptr)
{
if (MeshAsset.Object != nullptr)
{
SM->SetStaticMesh(MeshAsset.Object);
SM->SetGenerateOverlapEvents(true);
}
SM->SetMobility(EComponentMobility::Movable);
}
}
  1. Implement the Flee function in the .cpp file:
void APeasant::Flee(AKing* DeadKing)
{
// Display message on the screen
GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red,
TEXT("Waily Waily!"));

// Get the direction away from the dead king
FVector FleeVector = GetActorLocation() -
DeadKing->GetActorLocation();

// Set the magnitude (length) of the vector to 1
FleeVector.Normalize();

// Make the vector 500 times longer
FleeVector *= 500;

// Set the Actor's new location
SetActorLocation(GetActorLocation() + FleeVector);
}
  1. Return to the Unreal Editor and compile your scripts.
  1. Afterward, create a Blueprint class based on APeasant. You can do this by right-clicking on the Peasant object in the Content Browser and then selecting Create Blueprint class based on Peasant. Call the new BPPeasant class. Afterward, click on the Create Blueprint Class button:

  1. Within the Blueprint, click on the Event Graph tab and move upward to the Event BeingPlay node. Click and drag it away from the white (execution) pin of your BeginPlay node. Type get all, and you should see Get All Actors Of Class. Select the node to place it in your graph:

  1. Set the value of the purple (class) node to King. You can type king in the search bar to make locating the class in the list easier:

  1. Drag the blue grid (object array) node out into an empty space and, from the Actions menu that pops up, type in the word get. From the options that are available, select the Get (a copy) option:

  1. Drag away from the blue output pin of the get node and place a Not Equal (object) node:

  1. Connect the red (bool) pin of the Not Equal node to a Branch node, and wire the execution pin of Branch to our Get All Actors Of Class node:

  1. Connect the True pin of the branch to the Bind Event to OnKing Death node:

    Note that you will probably have to untick Context Sensitive in the context menu for the Bind Event node to be visible.
    1. Then, connect the output of the Get node into the Target property of the Bind Event to OnKingDeath node:

    Connecting the  Get node into the Target property of the Bind Event to OnKingDeath node
    If you double-click on a connection, you can create a reroute node that you can drag to make it easier to see the connections between nodes.
    1. Drag out the red pin of the Bind Event to OnKingDeath node and select Add Custom Event.... Give your event the desired name:
    You may need to uncheck the Context Sensitive option to see the Add Custom Event... option.

    Connecting the Custom Event and the Event Binding.
    1. Connect the white execution pin for the Custom Event to a new node named Flee, which we created back in Step 10:

    1. Lastly, drag the Dead King property from the Custom Event into the Dead King property of the Flee node.
    1. Verify that your Blueprint looks like what's shown in the following screenshot:

    The completed Blueprint
    1. Drag a copy of your King class into the level, and then add a few BPPeasant instances around it in a circle:

    1. Open the level Blueprint. Inside it, drag away from BeginPlay, and add a Delay node. Set the delay to 5 seconds:

    1. With your King instance that's selected in the level, right-click in the graph editor for the Level Blueprint.
    2. Select Call function on King 1 and look in the King category for a function called Die:

    1. Select Die, then connect its execution pin to the output execution pin from the delay:

    1. When you play your level, you should see that the king dies after 5 seconds:

    Afterward, you should see the peasants all wail and flee directly away from the king: