[UE4]代理事件(C++)

用宏定义相似格式:html

DECLARE_DELEGATE   //普通代理
DECLARE_DYNAMIC_DELEGATE_TwoParams   //动态代理
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams   //动态多广播代理
 
//多出的两个关键字的做用
In the case of multicast delegates,
 any number of entities within your code base can respond to the same event and receive the inputs and use them.

In the case of dynamic delegates,
 the delegate can be saved/loaded within a Blueprint graph (they're called Events/Event Dispatcher in BP).

DYNAMIC:能够在蓝图里被序列化后定义和绑定操做。函数

MULTICAST:可实现一个事件类型多函数广播(事件类型必须声明为蓝图类型)this

XXXParams:(使用DYNAMIC时)参数个数,宏定义参数里一个参数类型对应一个参数名字。spa

 

//声明位置(巩固下C++基础)代理

若是用到了静态事件类型的变量,由于须要在类外部声明这个变量,因此宏定义代理须要放在类外部。不然,随便放哪都行(在使用以前)。code

 

注意:htm

1.当使用MULTICAST时,声明的代理事件类型须要声明为蓝图类型。否则报错,如:对象

//声明代理
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FLoadDesignsDelegateEvent, const TArray<FDesignInfo>&,
 _designsInfo, const bool, _result);
 
//声明代理事件类型
static FLoadDesignsDelegateEvent m_OnLoadDesignsEve;
 
//调用代理事件
UFUNCTION(BlueprintCallable, Category = "BZone|SaveGameSystem")
static
 void GetOneDesignFromServer(const FString &_userId, const FString& _desingId,const FLoadDesignDelegateEvent& _eve);
 
//报错
F:/UE4_Projects/CGroupSystem_Server/Plugins/SaveGamePlugin/Source/SaveGamePlugin/SaveGame/Public/SaveGameBPLibrary.h(75)
 : Type '{multicast delegate type}' is not supported by blueprint. Function: GetAllDesignsFromServer Parameter _event

2.调用时先检查下是否绑定了代理blog

RamaMeleeWeapon_OnHit.IsBound() 

新版本的:m_DelegateEvent.ExecuteIfBound(params...);事件

3.UObject和C++的不一样绑定

 不是MULTICAST:

//UObject
RamaMeleeWeaponComp->RamaMeleeWeapon_OnHit.BindUObject(this,&USomeClass::RespondToMeleeDamageTaken);
//C++
RamaMeleeWeaponComp->RamaMeleeWeapon_OnHit.BindRaw(this,&FSomeRawCPPClass::RespondToMeleeDamageTaken);

是MULTICAST:

TScriptDelegate<> t_de1;

t_de1.BindUFunction(this, STATIC_FUNCTION_FNAME(TEXT("ADrawHouseManager::Mouse_LeftOneClick")));

m_leftOneClickEvents.Add(t_de1);

而且Mouse_LeftOneClickDown();须要UFUNCTION()修饰,不然事件能够添加进代理,但不会进入函数(不在UE的函数列表里因此没法找到并执行)。

 封装后的:

TMap<TMulticastScriptDelegate<>*, TScriptDelegate<>> m_bindEvents;

void ADrawHouseManager::AddBindEvent(TMulticastScriptDelegate<>* const _dele, const FString& _functionName)  
{     
    if (_dele!=nullptr&&!_functionName.IsEmpty())  
    {  
        TScriptDelegate<> t_de;  
        FString t_str = "ADrawHouseManager::" + _functionName;  
        t_de.BindUFunction(this, STATIC_FUNCTION_FNAME(*t_str));  
        _dele->Add(t_de);  
        m_bindEvents.Add(_dele, t_de);  
    }     
}  

别忘记对象销毁时在析构函数里调用解绑!!!

void ADrawHouseManager::UnBindAllInputEventsFromPC()  
{  
    if (m_bindEvents.Num()>0)  
    {  
        for (auto it:m_bindEvents)  
        {  
            TMulticastScriptDelegate<>* t_dele = it.Key;  
            if (t_dele!=nullptr)  
            {  
                t_dele->Remove(it.Value);  
            }             
        }  
    }  
}  
ADrawHouseManager::~ADrawHouseManager()  
{  
    UnBindAllInputEventsFromPC();  
}  

其他细节见:

https://wiki.unrealengine.com/Delegates_In_UE4,_Raw_Cpp_and_BP_Exposed  //C++代理事件

https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Delegates/index.html  

相关文章
相关标签/搜索