UE4 事件委託

2021-09-23 07:56:00 字數 1278 閱讀 1042

(1)先在標頭檔案上面宣告乙個**,名字要以f開頭

//宣告乙個事件委託

//第乙個引數事件的名稱(自己定義)

//後面的引數是事件所需要的引數

//因為有6個引數 所以新增_sixparams

declare_dynamic_multicast_delegate_sixparams(fonhealthchangesignature, usheathcomponent*, healthcomponent, float, health, float, healthdelta, const class udamagetype*, damagetype, class acontroller*, instigatedby, aactor*, damagecauser);

(2)宣告乙個**變數

public:

//建立事件例項

//屬性暴露給藍圖

uproperty(blueprintassignable, category = "events")

fonhealthchangesignature onhealthchange;

(3)在要呼叫的地方寫

//事件廣播

onhealthchange.broadcast(this,currenthealth,damage,damagetype,instigatedby, damagecauser);

(4)最後要在藍圖類中,選中宣告的委託c++類元件,找到委託事件進行繫結

或者在c++進行繫結

//.**件

uproperty(visibleanywhere,blueprintreadonly,category="health")

usheathcomponent* healthcomponent;

//要繫結的函式引數要和宣告委託的引數一致

ufunction()

void onhealthchange(usheathcomponent* healthcomponent, float health, float healthdelta, const class udamagetype* damagetype, class acontroller* instigatedby, aactor* damagecauser);

//.cpp

healthcomponent->onhealthchange.adddynamic(this, &ascharacter::onhealthchange);

UE4委託入門

ue4在delegatecombinations.h定義了各種型別的委託給我們使用。主要型別有 單播委託 多播委託 動態委託 可返回值委託 不同型別委託有的也可以組合出現。指的是只能繫結乙個函式的委託,函式名字中不含multicast。declare delegate oneparam單引數委託 d...

UE4之單播委託

參考 實現乙個簡單的demo,主要通過拉近拉遠來實現燈光的控制。實現邏輯如下 定義乙個委託 這裡我定義的是乙個有引數的委託 declare delegate oneparam mydelegate,bool uclass class tarraytest api atarraytestgamemod...

UE4之多播委託

參考 概念 這裡我計畫使用多播委託開兩個燈 多播委託的定義 declare dynamic multicast sparse delegate oneparam 定義的委託的型別必須是f開頭 所以我定義的函式如下 declare dynamic multicast delegate oneparam...