How it works...

We create a new actor (based on StaticMeshActor for convenience, as it saves us having to declare or create a Static Mesh component for the Actor visual representation).

We declare a dynamic multicast delegate using the DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam macro. Dynamic multicast delegates allow an arbitrary number of objects to subscribe (listen) and unsubscribe (stop listening) so that they will be notified when the delegate is broadcast.

The macro takes a number of arguments – the type name of the new delegate signature being created, the type of the signature's parameter, then the name of the signature's parameter.

We also add a function to King that will allow us to tell it to die. Because we want to expose the function to Blueprints for prototyping, we mark it as BlueprintCallable.

The DECLARE_DYNAMIC_MULTICAST_DELEGATE macro that we used earlier only declared a type; it didn't declare an instance of the delegate, so we do that now, referencing the type name that we provided earlier when invoking the macro.

Dynamic multicast delegates can be marked as BlueprintAssignable in their UPROPERTY declaration. This indicates to Unreal that the Blueprint system can dynamically assign events to the delegate that will be called when the delegate's Broadcast function is called.

As always, we assign a simple mesh to our King so that it has a visual representation in the game scene.

Within the Die function, we call Broadcast on our own delegate. We specified that the delegate would have a parameter that is a pointer to the king which died, so we pass this pointer as a parameter to the broadcast function.

If you want the king to be destroyed, rather than playing an animation or other effect when it dies, you need to change the delegate's declaration and pass in a different type. For example, you could use FVector, and simply pass in the location of the dead king directly so that the peasants could still flee appropriately.

Without this, you potentially could have a situation where the King pointer is valid when Broadcast is called, but the call to Actor::Destroy() invalidates it before your bound functions are executed.

Within our next StaticMeshActor subclass, called Peasant, we initialize the static mesh component as usual using a different shape from the one that we used for the King.

Inside the implementation of the peasant's Flee function, we simulate the peasants playing sound by printing a message on the screen.

We then calculate a vector to make the peasants flee by first finding a vector from the dead king to this peasant's location.

We normalize the vector to retrieve a unit vector (with a length of 1) pointing in the same direction.

Scaling the normalized vector and adding it to our current location calculates a position at a fixed distance, in the exact direction for the peasant to be fleeing directly away from the dead king.

SetActorLocation is then used to actually teleport the peasants to that location.

If you used a Character with an AI controller, you could have the Peasant pathfind to the target location rather than teleporting. Alternatively, you could use a Lerp function that's invoked during the peasant's Tick to make them slide smoothly rather than jump directly to the location.