Event Dispatcher
Source: Alex Quevillon - How To Create Event Dispatcher With C++
.h
#include "Kismet/BlueprintFunctionLibrary.h" // Engine
#include "Delegates/DelegateCombinations.h" // Core
#include "CreateEventDispatcher.generated.h"
// Delegate that can bind to multiple UFUNCTIONs simultaneously
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FMultiDispatcher);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMultiDispatcher_OneParam, bool, MyBool);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FMultiDispatcher_TwoParams, bool, MyBool, float, MyFloat);
// DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams
// DECLARE_DYNAMIC_MULTICAST_DELEGATE_FourParams
// DECLARE_DYNAMIC_MULTICAST_DELEGATE_FiveParams
// ...
UCLASS()
class UCreateEventDispatcher : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public: // These are the event dispatchers
UPROPERTY(BlueprintAssignable, Category = "Alex Quevillon|044 - Create Event Dispatcher")
FMultiDispatcher MultiDispatcher;
UPROPERTY(BlueprintAssignable, Category = "Alex Quevillon|044 - Create Event Dispatcher")
FMultiDispatcher_OneParam MultiDispatcher_OneParam;
UPROPERTY(BlueprintAssignable, Category = "Alex Quevillon|044 - Create Event Dispatcher")
FMultiDispatcher_TwoParams MultiDispatcher_TwoParams;
private: // These are the functions that will be attached to the event dispatchers
UFUNCTION()
void OnMultiDispatcherCalled();
UFUNCTION()
void OnMultiDispatcher_OneParamCalled(bool MyBool);
UFUNCTION()
void OnMultiDispatcher_TwoParamsCalled(bool MyBool, float MyFloat);
private:
// If you want to delegates to be Blueprint Assignable, they cannot be static.
// This is why we're using a Singleton for our tests today
static UCreateEventDispatcher* EventDispatcherSingleton;
// Set Singleton in constructor and destructor
UCreateEventDispatcher();
~UCreateEventDispatcher();
public:
/**
* Get the object you can use to bind yourself to the event dispatchers
*/
UFUNCTION(BlueprintPure, Category = "Alex Quevillon|044 - Create Event Dispatcher")
static UCreateEventDispatcher* GetEventDispatcherSingleton();
public:
/**
* Bind all the event dispatchers
*/
UFUNCTION(BlueprintCallable, Category = "Alex Quevillon|044 - Create Event Dispatcher")
static void BindEventDispatchersCpp();
/**
* Unbind all the event dispatchers
*/
UFUNCTION(BlueprintCallable, Category = "Alex Quevillon|044 - Create Event Dispatcher")
static void UnbindEventDispatchersCpp();
/**
* Call all the event dispatchers
*/
UFUNCTION(BlueprintCallable, Category = "Alex Quevillon|044 - Create Event Dispatcher")
static void CallEventDispatchers();
};
.cpp
#include "UCreateEventDispatcher.h"
UCreateEventDispatcher* UCreateEventDispatcher::EventDispatcherSingleton = nullptr;
UCreateEventDispatcher::UCreateEventDispatcher()
{
// Set the singleton
EventDispatcherSingleton = this;
}
UCreateEventDispatcher::~UCreateEventDispatcher()
{
// Set the singleton
EventDispatcherSingleton = nullptr;
}
UCreateEventDispatcher* UCreateEventDispatcher::GetEventDispatcherSingleton()
{
return UCreateEventDispatcher::EventDispatcherSingleton;
}
void UCreateEventDispatcher::OnMultiDispatcherCalled()
{
UE_LOG(LogTemp, Log, TEXT("OnMultiDispatcherCalled"));
}
void UCreateEventDispatcher::OnMultiDispatcher_OneParamCalled(bool MyBool)
{
UE_LOG(LogTemp, Log, TEXT("OnMultiDispatcher_OneParamCalled - %s"), *FString(MyBool ? "True" : "False"));
}
void UCreateEventDispatcher::OnMultiDispatcher_TwoParamsCalled(bool MyBool, float MyFloat)
{
UE_LOG(LogTemp, Log, TEXT("OnMultiDispatcher_TwoParamsCalled - %s %f"), *FString(MyBool ? "True" : "False"), MyFloat);
}
void UCreateEventDispatcher::BindEventDispatchersCpp()
{
// Because event dispatchers are on an object
UCreateEventDispatcher* Singleton = GetEventDispatcherSingleton();
// Bind event dispatcher - AddUniqueDynamic / AddDynamic (if it can be added multiple times)
Singleton->MultiDispatcher.AddUniqueDynamic(Singleton, &UCreateEventDispatcher::OnMultiDispatcherCalled);
Singleton->MultiDispatcher_OneParam.AddUniqueDynamic(Singleton, &UCreateEventDispatcher::OnMultiDispatcher_OneParamCalled);
Singleton->MultiDispatcher_TwoParams.AddUniqueDynamic(Singleton, &UCreateEventDispatcher::OnMultiDispatcher_TwoParamsCalled);
}
void UCreateEventDispatcher::UnbindEventDispatchersCpp()
{
// Because event dispatchers are on an object
UCreateEventDispatcher* Singleton = GetEventDispatcherSingleton();
// Unbind event dispatchers
Singleton->MultiDispatcher.RemoveDynamic(Singleton, &UCreateEventDispatcher::OnMultiDispatcherCalled);
Singleton->MultiDispatcher_OneParam.RemoveDynamic(Singleton, &UCreateEventDispatcher::OnMultiDispatcher_OneParamCalled);
Singleton->MultiDispatcher_TwoParams.RemoveDynamic(Singleton, &UCreateEventDispatcher::OnMultiDispatcher_TwoParamsCalled);
}
void UCreateEventDispatcher::CallEventDispatchers()
{
// Because event dispatchers are on an object
UCreateEventDispatcher* Singleton = GetEventDispatcherSingleton();
// Call event dispatchers
Singleton->MultiDispatcher.Broadcast();
Singleton->MultiDispatcher_OneParam.Broadcast(FMath::RandBool());
Singleton->MultiDispatcher_TwoParams.Broadcast(FMath::RandBool(), FMath::RandRange(0.0f, 100.0f));
}