UE进阶实例43(UE中的委托、事件、单组播)
UE中总计有7总类型的事件,每种的⽤途和功能各异,具体(⼀个参数作为参考):
>>UE中的任意委托或事件遵循UE的反射机制,需要使⽤UFUNCTION()标记回调⽅法
#1.单播,只能1对1
API 源码:
/**
* Declares a delegate that can only bind to one native function at a time
*
* @note: The last parameter is variadic and is ud as the 'template args' for this delegate's class (__VA_ARGS__)
* @note: To avoid issues with macro expansion breaking code navigation, make sure the type/class name macro params are unique across all of the m acros
*/
#define FUNC_DECLARE_DELEGATE( DelegateName, ReturnType,...)\
艾叶的功效和作用typedef TDelegate<ReturnType(__VA_ARGS__)> DelegateName;
定义语法:DECLARE_DELEGATE_OneParam
绑定函数:
DelegateOneParamFunc DefineDelegateOneParam;
DefineDelegateOneParam.BindSP()// 绑定智能指针
DefineDelegateOneParam.BindLambda()// 绑定lambda
DefineDelegateOneParam.BindRaw()// 绑定原⽣⽅法
DefineDelegateOneParam.BindStatic()// 绑定静态⽅法
DefineDelegateOneParam.BindUFunction()// 绑定到UObject中的⽅法
太阳蛋的做法
DefineDelegateOneParam.BindUObject()// 绑定到UObject对象
DefineDelegateOneParam.BindWeakLambda()// 绑定到弱引⽤lambda
使⽤⽅法:
DefineDelegateOneParam.Execute()// 执⾏
DefineDelegateOneParam.ExecuteIfBound()// 执⾏(如果有绑定对象)
⽤途指数: (基本没⽤)
#2. 多播或叫组播,1对多的关系,调⽤者可以⼴播,⽆界限调⽤,蓝图不可⽤
API 源码:
/** Declares a broadcast delegate that can bind to multiple native functions simultaneously */
#define FUNC_DECLARE_MULTICAST_DELEGATE( MulticastDelegateName, ReturnType,...)\
typedef TMulticastDelegate<ReturnType(__VA_ARGS__)> MulticastDelegateName;
定义语法:DECLARE_MULTICAST_DELEGATE_OneParam(MultiDelegateOneParamFunc, int)
绑定函数:
MultiDelegateOneParamFunc DefineMultiDelegateOneParam;
DefineMultiDelegateOneParam.AddSP()// 组播使⽤Add
DefineMultiDelegateOneParam.AddLambda()
DefineMultiDelegateOneParam.AddRaw()
DefineMultiDelegateOneParam.AddStatic()
DefineMultiDelegateOneParam.AddUFunction()
求职陷阱
DefineMultiDelegateOneParam.AddUObject()
DefineMultiDelegateOneParam.AddWeakLambda()
使⽤⽅法:
DefineMultiDelegateOneParam.Broadcast(1)// ⼴播出去
⽤途指数:
点评:因为纯C++层的多播,蓝图端监听不到
#3. UE中的事件,可⽤于异步,1对多,主体⼴播
API 源码:
/**
* Declares a multicast delegate that is meant to only be activated from OwningType
*
* @note: This behavior is not enforced and this type should be considered deprecated for new delegates, u normal multicast instead */
#define FUNC_DECLARE_EVENT( OwningType, EventName, ReturnType,...)\
class EventName :public TMulticastDelegate<ReturnType(__VA_ARGS__)>\
{\
friend class OwningType;\
};
语法定义:
// 第⼀个参数是类型,使⽤了这个事件的类,只能从这个类⼴播事件,约束范围
DECLARE_EVENT_OneParam(AUESocketGameModeBa, EventOneParam,int)
绑定函数:
EventOneParam DefineEventOnParam;
DefineEventOnParam.AddSP()
DefineEventOnParam.AddLambda()
DefineEventOnParam.AddRaw()
DefineEventOnParam.AddStatic()
DefineEventOnParam.AddUFunction()
DefineEventOnParam.AddUObject()
DefineEventOnParam.AddWeakLambda()
DefineEventOnParam.AddThreadSafeSP()//添加到线程安全智能智能(异步)
使⽤⽅法:DefineEventOnParam.Broadcast(1)
⽤途指数:
大腿根部酸痛点评:因为纯C++层的多播,蓝图端监听不到,底层框架使⽤⽐较好
#4. 动态单播,动态是指在运⾏时可以绑定
API 源码:
/** Declare ur's dynamic delegate, with wrapper proxy method for executing the delegate */
#define FUNC_DECLARE_DYNAMIC_DELEGATE( TWeakPtr, DynamicDelegateClassName, ExecFunction, FuncParamList, FuncParamPassThru,...)\ class DynamicDelegateClassName :public TBaDynamicDelegate<TWeakPtr, __VA_ARGS__>\
{\
public:\
/** Default constructor */\
DynamicDelegateClassName()\
消费心理学案例{\
}\
\
/** Construction from an FScriptDelegate must be explicit. This is really only ud by UObject system internals. */\
explicit DynamicDelegateClassName(const TScriptDelegate<>& InScriptDelegate )\
: TBaDynamicDelegate<TWeakPtr, __VA_ARGS__>( InScriptDelegate )\
{\
}\
\
/** Execute the delegate. If the function pointer is not valid, an error will occur. */\
inline void Execute( FuncParamList )const\
{\
/* Verify that the ur object is still valid. We only have a weak reference to it. */\
checkSlow(IsBound());\
ExecFunction( FuncParamPassThru );\
}\
/** Execute the delegate, but only if the function pointer is still valid */\
inline bool ExecuteIfBound( FuncParamList )const\
{\
if(IsBound())\
{\
ExecFunction( FuncParamPassThru );\
return true;\
}\
return fal;\
}\
};
定义语法:DECLARE_DYNAMIC_DELEGATE_OneParam(DynamicDelegateOnePrarm, int, InInt);
绑定⽅法:
DynamicDelegateOnePrarm DefineDynamicDelegateOneParam;
DefineDynamicDelegateOneParam.BindDynamic()//动态绑定
DefineDynamicDelegateOneParam.BindUFunction()
使⽤⽅法:
DefineDynamicDelegateOneParam.Execute(1)
DefineDynamicDelegateOneParam.ExecuteIfBound(1)
⽤途指数:
点评:动态的蓝图端可监听,但是1对1,基本没⽤
#5. 动态多播,使⽤最多,功能全⾯
API 源码:积极生活
/** Declare ur's dynamic multi-cast delegate, with wrapper proxy method for executing the delegate */
#define FUNC_DECLARE_DYNAMIC_MULTICAST_DELEGATE(TWeakPtr, DynamicMulticastDelegateClassName, ExecFunction, FuncParamList, FuncPa ramPassThru,...)\
class DynamicMulticastDelegateClassName :public TBaDynamicMulticastDelegate<TWeakPtr, __VA_ARGS__>\
{\
public:\
/** Default constructor */\
DynamicMulticastDelegateClassName()\
{\
}\
\
/** Construction from an FMulticastScriptDelegate must be explicit. This is really only ud by UObject system internals. */\
explicit DynamicMulticastDelegateClassName(const TMulticastScriptDelegate<>& InMulticastScriptDelegate )\
: TBaDynamicMulticastDelegate<TWeakPtr, __VA_ARGS__>( InMulticastScriptDelegate )\
{\
}\
\
/** Broadcasts this delegate to all bound objects, except to tho that may have expired */\
void Broadcast( FuncParamList )const\
{\
ExecFunction( FuncParamPassThru );\
}\
};
定义语法:DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(DynamicMultiDelegateOneParam, int, InInt);
绑定⽅法:
⽤途指数:
点评:动态的蓝图端可监听,蓝图C++全部可以监听,调⽤者可以⼴播
DynamicMultiDelegateOneParam DefineDynamicDelegateOneParam;
DefineDynamicDelegateOneParam.AddDynamic()//动态绑定
DefineDynamicDelegateOneParam.AddUnique()//唯⼀绑定,再次绑定同样的⽅法⽆效
杨门女将军令如山
DefineDynamicDelegateOneParam.AddUniqueDynamic()//动态绑定的时候唯⼀,已经绑定的有效
使⽤⽅法:DefineDynamicDelegateOneParam.Broadcast(1);
#6. 带返回值类型的单播
API源码:
/**
* Declares a delegate that can only bind to one native function at a time
*
* @note: The last parameter is variadic and is ud as the 'template args' for this delegate's class (__VA_ARGS__)
* @note: To avoid issues with macro expansion breaking code navigation, make sure the type/class name macro params are unique across all of the m acros
*/
#define FUNC_DECLARE_DELEGATE( DelegateName, ReturnType,...)\
typedef TDelegate<ReturnType(__VA_ARGS__)> DelegateName;
定义语法:
// 第⼀个参数是返回值类型
DECLARE_DELEGATE_RetVal_OneParam(int, DelegateRetValOneParam,int)
绑定⽅法:
DelegateRetValOneParam DefineDelegateRetValOneParam;
DefineDelegateRetValOneParam.BindSP()
DefineDelegateRetValOneParam.BindRaw()
DefineDelegateRetValOneParam.BindUFunction()
DefineDelegateRetValOneParam.BindUObject()
DefineDelegateRetValOneParam.BindStatic()
DefineDelegateRetValOneParam.BindThreadSafeSP()// 底层框架异步智能指针⽤的
DefineDelegateRetValOneParam.BindLambda()
DefineDelegateRetValOneParam.BindWeakLambda()
⽤途指数:
点评:1对1,蓝图不可⽤,基本没⽤
#7. 带返回值类型的多播
API源码:
民开头的成语
/** Declare ur's dynamic delegate with return value, with wrapper proxy method for executing the delegate */
#define FUNC_DECLARE_DYNAMIC_DELEGATE_RETVAL(TWeakPtr, DynamicDelegateRetValClassName, ExecFunction, RetValType, FuncParamList, F uncParamPassThru,...)\
class DynamicDelegateRetValClassName :public TBaDynamicDelegate<TWeakPtr, __VA_ARGS__>\
{\
public:\
/** Default constructor */\
DynamicDelegateRetValClassName()\
{\
}\
\
/
** Construction from an FScriptDelegate must be explicit. This is really only ud by UObject system internals. */\
explicit DynamicDelegateRetValClassName(const TScriptDelegate<>& InScriptDelegate )\
: TBaDynamicDelegate<TWeakPtr, __VA_ARGS__>( InScriptDelegate )\
{\
}\
\
/** Execute the delegate. If the function pointer is not valid, an error will occur. */\
inline RetValType Execute( FuncParamList )const\
{\
/* Verify that the ur object is still valid. We only have a weak reference to it. */\
checkSlow(IsBound());\
return ExecFunction( FuncParamPassThru );\
}\
};
定义语法:
//返回值类型、名称、参数类型、参数名
DECLARE_DYNAMIC_DELEGATE_RetVal_OneParam(int, DynamicDelegateRetValOneParam,int, InInt);
绑定⽅法:
DynamicDelegateRetValOneParam DefineDynamicDelegateRetValOneParam;
DefineDynamicDelegateRetValOneParam.BindDynamic()
DefineDynamicDelegateRetValOneParam.BindUFunction()
⽤途指数:
点评:1对多,蓝图可⽤,框架⽤⽐较好
7种委托事件类型,3种⽐较常⽤,1种最好⽤,带返回值的基本不⽤
// Multiple-parameter versions of above delegate types:
#define DECLARE_DELEGATE_OneParam( DelegateName, Param1Type )FUNC_DECLARE_DELEGATE( DelegateName,void, Param1Type )
#define DECLARE_MULTICAST_DELEGATE_OneParam( DelegateName, Param1Type )FUNC_DECLARE_MULTICAST_DELEGATE( DelegateName,vo id, Param1Type )
#define DECLARE_EVENT_OneParam( OwningType, EventName, Param1Type )FUNC_DECLARE_EVENT( OwningType, EventName,void, Param1Typ e )
#define DECLARE_DYNAMIC_DELEGATE_OneParam( DelegateName, Param1Type, Param1Name )BODY_MACRO_COMBINE(CURRENT_FILE_ID,_,_ _LINE__,_DELEGATE)FUNC_DECLARE_DYNAMIC_DELEGATE( FWeakObjectPtr, DelegateName, DelegateName##_DelegateWrapper,FUNC_CONCA T( Param1Type InParam1 ),FUNC_CONCAT(*this, InParam1 ),void, Param1Type )
#define DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam( DelegateName, Param1Type, Param1Name )BODY_MACRO_COMBINE(CURREN T_FILE_ID,_,__LINE__,_DELEGATE)FUNC_DECLARE_DYNAMIC_MULTICAST_DELEGATE( FWeakObjectPtr, DelegateName, DelegateName##_Deleg ateWrapper,FUNC_CONCAT( Param1Type InParam1 ),FUNC_CONCAT(*this, InParam1 ),void, Param1Type )
#define DECLARE_DELEGATE_RetVal_OneParam( ReturnValueType, DelegateName, Param1Type )FUNC_DECLARE_DELEGATE( DelegateName, Ret urnValueType, Param1Type )
#define DECLARE_DYNAMIC_DELEGATE_RetVal_OneParam( ReturnValueType, DelegateName, Param1Type, Param1Name )BODY_MACRO_COMBI NE(CURRENT_FILE_ID,_,__LINE__,_DELEGATE)FUNC_DECLARE_DYNAMIC_DELEGATE_RETVAL( FWeakObjectPtr, DelegateName, DelegateName ##_DelegateWrapper, ReturnValueType,FUNC_CONCAT( Param1Type InParam1 ),FUNC_CONCAT(*this, InParam1 ), ReturnValueType, Param1Type )