用宏定義相似格式: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