UE4之Delegate:动态单播

本文详细介绍了UE4中动态单播Delegate的定义、使用和实现原理,包括FUNC_DECLARE_DYNAMIC_DELEGATE宏、TBaseDynamicDelegate基类的AddDynamic方法、ProcessDelegate函数的执行流程,以及动态委托在实际应用中的参数传递方式。
摘要由CSDN通过智能技术生成

定义

#define FUNC_CONCAT( ... ) __VA_ARGS__

#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 )

定义一个动态单播,UHT会根据BODY_MACRO_COMBINE生成一个FILE_ID_LINE_DELEGATE宏,这个函数留到后面说。
先看FUNC_DECLARE_DYNAMIC_DELEGATE 宏定义

#define FUNC_DECLARE_DYNAMIC_DELEGATE( TWeakPtr, DynamicDelegateName, ExecFunction, FuncParamList, FuncParamPassThru, ... ) \
	class DynamicDelegateName : public TBaseDynamicDelegate<TWeakPtr, __VA_ARGS__> \
	{ \
	public: \
		DynamicDelegateName() \
		{ \
		} \
		\
		/** Construction from an FScriptDelegate must be explicit.  This is really only used by UObject system internals. */ \
		explicit DynamicDelegateName( 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() ); \
			ExecFunction( FuncParamPassThru ); \
		} \
		/** Execute the delegate, but only if the function pointer is still valid */ \
		inline bool ExecuteIfBound( FuncParamList ) const \
		{
    \
			if</
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UE4中,您可以使用单播委托(Unicast Delegate)来实现事件的触发和处理。单播委托允许将一个或多个方法注册为事件的处理程序,并且可以使用payload参数传递数据。 以下是在UE4中使用单播委托和payload参数的示例: 1. 在您的类声明中定义委托类型和委托实例。例如: ```cpp DECLARE_DELEGATE_OneParam(FMyDelegate, const FString&); FMyDelegate MyDelegate; ``` 2. 在需要触发事件的地方,调用委托实例并传递payload参数。例如: ```cpp FString Payload = "Hello, World!"; MyDelegate.Execute(Payload); ``` 3. 在其他地方注册方法作为事件的处理程序,并定义方法的签名与委托类型一致。例如: ```cpp void MyClass::MyMethod(const FString& Payload) { UE_LOG(LogTemp, Warning, TEXT("MyMethod executed with payload: %s"), *Payload); } // 注册方法作为处理程序 MyDelegate.BindUObject(this, &MyClass::MyMethod); ``` 在上面的示例中,我们声明了FMyDelegate作为委托类型,并创建了一个委托实例MyDelegate。然后,在需要触发事件的地方,我们调用MyDelegate的Execute方法,并传递Payload参数。 在另一个类(例如MyClass)中,我们定义了一个名为MyMethod的方法,并使用BindUObject将其注册为处理程序。当事件触发时,MyMethod方法将被执行,并且可以访问传递的payload参数。 请注意,上述示例中的代码仅为演示目的,实际使用时您需要根据您的需求和代码结构进行适当的修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值