Events and Delegates - jgoffeney/Cesium4Unreal Wiki

Back

C++

To setup an event in C++:

Declaring a Delegate Function

Unreal has a set of macros to declare a delegate function. A separate macro exists for the number of variables in the signature of the function (up to 8). The first parameter is the delegate function name which has to start with an F. Then for each function variable add the parameter data type followed by the variable name. The variable names are what will appear in the blueprint for the event when it is listened for.

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FDelegateFunctionName0);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDelegateFunctionName1, <dataType>, <variableName>);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FDelegateFunctionName2, <dataType>, <variableName>, <dataType>, <variableName>);

Example

The macros have to added after the final #include and before the class definition.

#include "MainActor.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FMyDelegateFunction0);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMyDelegateFunction1, int, variableA);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FMyDelegateFunction2, int, variableA, bool, variableB);

UCLASS(Config = CesiumStarter)
class UNREALGEOSPATIAL_API AMainActor : public AActor
{
	GENERATED_BODY()
	
public:	

Defining a Variable

The "event" functions are declared like variables with the UPROPERTY macro with the BlueprintCallable and BlueprintAssignable to access them in the blueprint.

Example

UCLASS(Config = CesiumStarter)
class UNREALGEOSPATIAL_API AMainActor : public AActor
{
	GENERATED_BODY()
	
public:	
    UPROPERTY(BlueprintCallable, BlueprintAssignable, Category = "Event Dispatchers")
    FMyDelegateFunction0 OnDelegateFunction0;

    UPROPERTY(BlueprintCallable, BlueprintAssignable, Category = "Event Dispatchers")
    FMyDelegateFunction1 OnDelegateFunction1;

    UPROPERTY(BlueprintCallable, BlueprintAssignable, Category = "Event Dispatchers")
    FMyDelegateFunction2 OnDelegateFunction2;

Fire the Event

Events defined by Unreal delegates have a Broadcast method which takes the delegate variable values as parameters.

Example

These sample functions contain the sample event functions and calling them fire the event to broadcast the values.

void AMainActor::FireEvent0(){
  OnDelegateFunction0.Broadcast();
}

void AMainActor::FireEvent1(){
  int value1 = 255;
  OnDelegateFunction0.Broadcast(value1);
}

void AMainActor::FireEvent2(){
  int value1 = 300;
  bool value2 = false;
  OnDelegateFunction0.Broadcast(value1, value2);
}

Blueprint

Like any event you have to first bind it which you can do within the blueprint. On the class's blueprint Event Graph you can drag from the Event BeginPlay exec pin and add search on the event function name and select the version with Bind Event to included (for OnDelegateFunction0 add Bind To On Delegate Function 0). From the Event input add a Custom Event node which will automatically create output pins for the event variables. Use the Custom Event exec pin to create the event handler nodes.

Example

This example shows the three events bound with the BeginPlay event in turn and printing the values passed into each to the screen.

BlueprintDelegateEventExample