UE进阶实例43(UE中的委托、事件、单组播)

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 used as the 'template args' for this delegate's classes (__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 these m acros
tsf过载保护*/
#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, use normal multicast instead  */
#define FUNC_DECLARE_EVENT( OwningType, EventName, ReturnType,...)\
class EventName :public TMulticastDelegate<ReturnType(__VA_ARGS__)>\
{\
friend class OwningType;\
};
语法定义:
// 第⼀个参数是类型,使⽤了这个事件的类,只能从这个类⼴播事件,约束范围
DECLARE_EVENT_OneParam(AUESocketGameModeBase, 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 user's dynamic delegate, with wrapper proxy method for executing the delegate */
#define FUNC_DECLARE_DYNAMIC_DELEGATE( TWeakPtr, DynamicDelegateClassName, ExecFunction, FuncParamList, FuncParamPassThru,...)\ class DynamicDelegateClassName :public TBaseDynamicDelegate<TWeakPtr, __VA_ARGS__>\
{\
public:\
/** Default constructor */\
DynamicDelegateClassName()\
{\
}\
\
/** Construction from an FScriptDelegate must be explicit.  This is really only used by UObject system internals. */\
explicit DynamicDelegateClassName(const TScriptDelegate<>& InScriptDelegate )\
: TBaseDynamicDelegate<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 user object is still valid.  We only have a weak reference to it. */\
checkSlow(IsBound());\
cn-mExecFunction( 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 false;\
}\
};
定义语法:DECLARE_DYNAMIC_DELEGATE_OneParam(DynamicDelegateOnePrarm, int, InInt);
绑定⽅法:
DynamicDelegateOnePrarm DefineDynamicDelegateOneParam;
DefineDynamicDelegateOneParam.BindDynamic()//动态绑定
DefineDynamicDelegateOneParam.BindUFunction()
使⽤⽅法:
DefineDynamicDelegateOneParam.Execute(1)
DefineDynamicDelegateOneParam.ExecuteIfBound(1)
⽤途指数:
点评:动态的蓝图端可监听,但是1对1,基本没⽤
#5. 动态多播,使⽤最多,功能全⾯
API 源码:
/** Declare user'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 TBaseDynamicMulticastDelegate<TWeakPtr, __VA_ARGS__>\
{\
public:\
/** Default constructor */\
DynamicMulticastDelegateClassName()\
{\
}\
\
/** Construction from an FMulticastScriptDelegate must be explicit.  This is really only used by UObject system internals. */\
explicit DynamicMulticastDelegateClassName(const TMulticastScriptDelegate<>& InMulticastScriptDelegate )\
: TBaseDynamicMulticastDelegate<TWeakPtr, __VA_ARGS__>( InMulticastScriptDelegate )\
{\
}\
\
/** Broadcasts this delegate to all bound objects, except to those 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 used as the 'template args' for this delegate's classes (__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 these m acros
*/
#define FUNC_DECLARE_DELEGATE( DelegateName, ReturnType,...)\siro2246
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 user'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 TBaseDynamicDelegate<TWeakPtr, __VA_ARGS__>\
{\
public:\
/** Default constructor */\
DynamicDelegateRetValClassName()\
{\
}\
\
/
** Construction from an FScriptDelegate must be explicit.  This is really only used by UObject system internals. */\
explicit DynamicDelegateRetValClassName(const TScriptDelegate<>& InScriptDelegate )\
: TBaseDynamicDelegate<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 user 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 )

本文发布于:2024-09-23 05:30:55,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/266060.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:绑定   类型   事件   参数   智能   动态   返回值   委托
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议