Ue4_C++委托
五种委托形式
1.单播委托
2.多播委托
3.事件
4.动态单播委托
5.动态多播委托
DelegateCombinations.h
/** Declares a delegate that can only bind to one native function at a time */
#define DECLARE_DELEGATE( DelegateName ) FUNC_DECLARE_DELEGATE( DelegateName, void )
/** Declares a broadcast delegate that can bind to multiple native functions simultaneously */
#define DECLARE_MULTICAST_DELEGATE( DelegateName ) FUNC_DECLARE_MULTICAST_DELEGATE( DelegateName, void )
#define DECLARE_EVENT( OwningType, EventName ) FUNC_DECLARE_EVENT( OwningType, EventName, void )
/** Declares a blueprint-accessible delegate that can only bind to one UFUNCTION at a time */
#define DECLARE_DYNAMIC_DELEGATE( DelegateName ) BODY_MACRO_COMBINE(CURRENT_FILE_ID,_,__LINE__,_DELEGATE) FUNC_DECLARE_DYNAMIC_DELEGATE( FWeakObjectPtr, DelegateName, DelegateName##_DelegateWrapper, , FUNC_CONCAT( *this ), void )
/** Declares a blueprint-accessible broadcast delegate that can bind to multiple native UFUNCTIONs simultaneously */
#define DECLARE_DYNAMIC_MULTICAST_DELEGATE( DelegateName ) BODY_MACRO_COMBINE(CURRENT_FILE_ID,_,__LINE__,_DELEGATE) FUNC_DECLARE_DYNAMIC_MULTICAST_DELEGATE( FWeakObjectPtr, DelegateName, DelegateName##_DelegateWrapper, , FUNC_CONCAT( *this ), void )
Engine\Source\Runtime\Core\Public\Delegates
单播委托绑定
通过创建函数指针
DelegateSignatureImpl.inl
/** * Binds a UFunction-based member function delegate. * * UFunction delegates keep a weak reference to your object. * You can use ExecuteIfBound() to call them. */ template <typename UObjectTemplate, typename... VarTypes> inline void BindUFunction(UObjectTemplate* InUserObject, const FName& InFunctionName, VarTypes... Vars) { *this = CreateUFunction(InUserObject, InFunctionName, Vars...); }
/** * Binds a UObject-based member function delegate. * * UObject delegates keep a weak reference to your object. * You can use ExecuteIfBound() to call them. */ template <typename UserClass, typename... VarTypes> inline void BindUObject(UserClass* InUserObject, typename TMemFunPtrType<false, UserClass, RetValType (ParamTypes..., VarTypes...)>::Type InFunc, VarTypes... Vars) { static_assert(!TIsConst<UserClass>::Value, "Attempting to bind a delegate with a const object pointer and non-const member function."); *this = CreateUObject(InUserObject, InFunc, Vars...); } template <typename UserClass, typename... VarTypes> inline void BindUObject(const UserClass* InUserObject, typename TMemFunPtrType<true, UserClass, RetValType (ParamTypes..., VarTypes...)>::Type InFunc, VarTypes... Vars) { *this = CreateUObject(InUserObject, InFunc, Vars...); }
多播委托的绑定
/** * Adds a delegate instance to this multicast delegate's invocation list. * * @param Delegate The delegate to add. */ FDelegateHandle Add(const FDelegate& InNewDelegate) { FDelegateHandle Result; if (Super::GetDelegateInstanceProtectedHelper(InNewDelegate)) { Result = Super::AddDelegateInstance(CopyTemp(InNewDelegate)); } return Result; }
/** * Adds a UObject-based member function delegate. * * UObject delegates keep a weak reference to your object. * * @param InUserObject User object to bind to * @param InFunc Class method function address */ template <typename UserClass, typename... VarTypes> inline FDelegateHandle AddUObject(UserClass* InUserObject, typename TMemFunPtrType<false, UserClass, void (ParamTypes..., VarTypes...)>::Type InFunc, VarTypes... Vars) { static_assert(!TIsConst<UserClass>::Value, "Attempting to bind a delegate with a const object pointer and non-const member function."); return Add(FDelegate::CreateUObject(InUserObject, InFunc, Vars...)); }
remove方法
/** * Removes a delegate instance from this multi-cast delegate's invocation list (performance is O(N)). * * Note that the order of the delegate instances may not be preserved! * * @param Handle The handle of the delegate instance to remove. * @return true if the delegate was successfully removed. */ bool Remove( FDelegateHandle Handle ) { bool bResult = false; if (Handle.IsValid()) { bResult = RemoveDelegateInstance(Handle); } return bResult; }
动态委托:
动态单播委托
动态多播委托
声明宏 |
描述 |
---|---|
|
创建一个动态委托。 |
|
创建一个动态组播委托。 |
注:动态委托可序列化,其函数可按命名查找,但其执行速度比常规委托慢
多态委托简单使用
//动态委托可序列化,其函数可按命名查找,但其执行速度比常规委托慢
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTestDyMulDELEGATE);
DECLARE_DYNAMIC_DELEGATE(FTestDyDELEGATE);
UPROPERTY(BlueprintAssignable,BlueprintCallable)
FTestDyMulDELEGATE TestDyMulDELEGATE;
FTestDyDELEGATE TestDyDELEGATE;
UFUNCTION(BlueprintCallable)
void ExDyDELEGATE();
UFUNCTION(BlueprintCallable)
void LogFunc();
UFUNCTION(BlueprintCallable)
void CallExDyMul();
UFUNCTION()
void DyMul_Log();
ATraceCharacter.cpp
//绑定
TestDyDELEGATE.BindUFunction(this,TEXT("LogFunc"));
TestDyMulDELEGATE.AddDynamic(this,&ATraceCharacter::DyMul_Log);
void ATraceCharacter::LogFunc()
{
//单播
UE_LOG(LogTemp,Warning,TEXT("DyDELEGATE Excute"))
}
void ATraceCharacter::ExDyDELEGATE()
{
//单播动态委托调用
TestDyDELEGATE.ExecuteIfBound();
}
void ATraceCharacter::CallExDyMul()
{
TestDyMulDELEGATE.Broadcast();
}
void ATraceCharacter::DyMul_Log()
{
UE_LOG(LogTemp, Warning, TEXT("DyMul_Log"));
}
多态多播委托:可在蓝图中调用/c++ Broadcast 调用
单播动态委托