UEC++委托
单播代理,多播代理,动态代理
单播(基础)
// 无参无返回值
DECLARE_DELEGATE(DelegateName)
void Function();
// 无参有返回值
DECLARE_DELEGATE_RetVal(RetValType, DelegateName)
RetValType Function();
// 1-8个参数无返回值
DECLARE_DELEGATE_OneParam(DelegateName, Param1Type)
void Function(Param1);
DECLARE_DELEGATE_TwoParams(DelegateName, Param1Type, Param2Type)
void Function(Param1,Param2);
//...
// 1-8个参数有返回值
DECLARE_DELEGATE_RetVal_OneParam(RetValType, DelegateName, Param1Type)
RetValType Function(Param1);
DECLARE_DELEGATE_RetVal_ThreeParams(RetValType, DelegateName, Param1Type, Param2Type, Param3Type)
RetValType Function(Param1,Param2,Param3);
//...
多播与动态
- DECLARE_DYNAMIC_DELEGATE... // 动态
- DECLARE_MULTICAST_DELEGATE... // 多播
- DECLARE_DYNAMIC_MULTICAST_DELEGATE... // 动态多播
8种绑定委托的函数
方法 | 详解 |
---|---|
基础 | |
BindStatic | 绑定到全局静态函数 |
BindLambda | 绑定Lambda表达式 |
BindRaw | 绑定到一个原生C++类对象函数 |
BindSP | 绑定一个快速线程不安全共享指针函数 |
BindUObject | 绑定继承UObject 类的对象函数 |
高阶 | |
BindWeakLambda | 绑定弱对象的Lambda表达式 |
BindThreadSafeSP | 绑定一个较慢线程安全共享指针函数 |
BindUFunction | 绑定由UFUNCTION 标记的函数 |
BindStatic
原生c++指针全局函数委托
/**
* Binds a raw C++ pointer global function delegate
*/
template <typename... VarTypes>
inline void BindStatic(typename TBaseStaticDelegateInstance<FuncType, UserPolicy, VarTypes...>::FFuncPtr InFunc, VarTypes... Vars)
{
//...
}
Lambda
静态c++的Lambda委托
.BindLambda([=](FString str)
{
UE_LOG(LogTemp,Log,TEXT("Str:%s"),*Str);
});
源代码
/**
* Static: Binds a C++ lambda delegate
* technically this works for any functor types, but lambdas are the primary use case
*/
template<typename FunctorType, typename... VarTypes>
inline void BindLambda(FunctorType&& InFunctor, VarTypes... Vars)
{
//...
}
WeakLambda
静态弱对象c++Lambda委托
绑定一个匿名函数,在传入的UObject有效,还没有被回收的时候都可以调用这个匿名函数。这个匿名函数中可以用this,但是其他关键词不一定能用
/**
* Static: Binds a weak object C++ lambda delegate
* technically this works for any functor types, but lambdas are the primary use case
*/
template<typename UserClass, typename FunctorType, typename... VarTypes>
inline void BindWeakLambda(UserClass* InUserObject, FunctorType&& InFunctor, VarTypes... Vars)
使用这种委托可以当是有条件的匿名函数使用
Raw
原生c++指针委托
/**
* Binds a raw C++ pointer delegate.
*
* Raw pointer doesn't use any sort of reference, so may be unsafe to call if the object was
* deleted out from underneath your delegate. Be careful when calling Execute()!
*/
template <typename UserClass, typename... VarTypes>
inline void BindRaw(UserClass* InUserObject, typename TMemFunPtrType<false, UserClass, RetValType (ParamTypes..., VarTypes...)>::Type InFunc, VarTypes... Vars)
{
//...
}
SP
基于共享指针成员的 函数委托
/**
* Binds a shared pointer-based (fast, not thread-safe) member function delegate.
*
* Shared pointer delegates keep a weak reference to your object.
* You can use ExecuteIfBound() to call them.
*/
template <typename UserClass, typename... VarTypes>
inline void BindSP(const TSharedRef<UserClass, ESPMode::Fast>& InUserObjectRef, typename TMemFunPtrType<false, UserClass, RetValType (ParamTypes..., VarTypes...)>::Type InFunc, VarTypes... Vars)
{
//...
}
ThreadSafeSP
用一个弱指针TWeakPtr来绑定一个原生C++类成员函数,当指针SharedPtr指向的对象无效的时候不会执行绑定的回调函数
/**
* Binds a shared pointer-based (slower, conditionally thread-safe) member function delegate.
*
* Shared pointer delegates keep a weak reference to your object.
* You can use ExecuteIfBound() to call them.
*/
template <typename UserClass, typename... VarTypes>
inline void BindThreadSafeSP(const TSharedRef<UserClass, ESPMode::ThreadSafe>& InUserObjectRef, typename TMemFunPtrType<false, UserClass, RetValType (ParamTypes..., VarTypes...)>::Type InFunc, VarTypes... Vars)
{
//...
}
UFunction
基于UFunction的函数委托
用来绑定一个UObject的UFUNCTION函数,原生的与动态的委托都可以用这个函数来绑定回调函数
/**
* 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)
{
//...
}
UObject
基于UObject成员的函数委托
/**
* 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)
{
//...
}
定义事件
如果是重托定义在类定义中时,委托代表的就是事件
PayLoad 载荷数据
学习中...
评论