Ue4_C++委托
2022/4/10 12:12:32
本文主要是介绍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 调用
单播动态委托
这篇关于Ue4_C++委托的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升