UDelegate in Unreal Engine C++
#include "Delegates/Delegate.h"
DECLARE_DELEGATE_TwoParams(FMyDelegateA, int32 , float );
DECLARE_MULTICAST_DELEGATE_TwoParams(FMyDelegateB, int32 , float );
UDELEGATE()
DECLARE_DYNAMIC_DELEGATE_TwoParams(FMyDelegateC, int32, FirstVar, float, SecondVar);
UDELEGATE()
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FMyDelegateD, int32, FirstVar, float, SecondVar);
FMyDelegateA DelegateVarA;
FMyDelegateB DelegateVarB;
UPROPERTY()
FMyDelegateC DelegateVarC;
UPROPERTY(BlueprintAssignable, BlueprintCallable)
FMyDelegateD DelegateVarD;
void MyFunction(int32 Var1, float Var2);
UFUNCTION()
void AnotherFunction(int32 Var1, float Var2);
void AExampleActor::BeginPlay() {
Super::BeginPlay();
DelegateVarA.BindUObject(this, &AExampleActor::MyFunction);
DelegateVarA.Execute(23, 8.7);
bool IsBoundA = DelegateVarA.ExecuteIfBound(23, 8.7);
DelegateVarA.Unbind();
FDelegateHandle HandleB = DelegateVarB.AddUObject(this, &AExampleActor::MyFunction);
DelegateVarB.Broadcast(42, 3.14f);
DelegateVarB.Remove(HandleB);
DelegateVarB.RemoveAll(this);
DelegateVarB.Clear()
DelegateVarC.BindDynamic(this, &AExampleActor::AnotherFunction);
DelegateVarC.Execute(23, 8.7);
bool IsBoundC = DelegateVarC.ExecuteIfBound(23, 8.7);
DelegateVarC.Unbind();
DelegateVarD.AddDynamic(this, &AExampleActor::AnotherFunction);
DelegateVarD.Broadcast(23, 8.7);
DelegateVarD.RemoveDynamic(this, &AExampleActor::AnotherFunction);
DelegateVarD.RemoveAll(this);
DelegateVarD.Clear();
}