ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Ue4_C++委托

2022-04-10 12:00:56  阅读:366  来源: 互联网

标签:委托 void C++ DelegateName delegate Ue4 DECLARE DELEGATE


五种委托形式

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 调用

 

单播动态委托

 

 

标签:委托,void,C++,DelegateName,delegate,Ue4,DECLARE,DELEGATE
来源: https://www.cnblogs.com/zsymdbk/p/16122459.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有