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_DELEGATE[_RetVal, ...]( DelegateName )

创建一个动态委托。

DECLARE_DYNAMIC_MULTICAST_DELEGATE[_RetVal, ...]( DelegateName )

创建一个动态组播委托。

注:动态委托可序列化,其函数可按命名查找,但其执行速度比常规委托慢

 


多态委托简单使用

复制代码
//动态委托可序列化,其函数可按命名查找,但其执行速度比常规委托慢
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 调用

 

单播动态委托

 

 

posted on   Animer  阅读(332)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示