Unreal Engine 4 学习笔记(十一):Delegate & Event

一、Delegate

Delegate分为
1.Single-cast  Delegate
2.Multi-cast  Delegate
3.Dynamic  Delegate

其中Multi-cast Delegate中又包含Multi-cast Delegate Event,将在Event一节中再讲解。

delegate的签名支持如下形式
1.函数支持返回一个值
2.在绑定时支持最多4个payload变量
3.最多8个函数参数
4.函数支持宣称为const

Single-cast Delegate

对函数声明绑定的方式
void Function()                                               DECLARE_DELEGATE( DelegateName )
void Function( <Param1> )                                   DECLARE_DELEGATE_OneParam( DelegateName, Param1Type )
void Function( <Param1>, <Param2>, ... )   DECLARE_DELEGATE_<Num>Params( DelegateName, Param1Type, Param2Type, ... )
<RetVal> Function()                            DECLARE_DELEGATE_RetVal( RetValType, DelegateName )
<RetVal> Function( <Param1> )                  DECLARE_DELEGATE_RetVal_OneParam( RetValType, DelegateName, Param1Type )
<RetVal> Function( <Param1>, <Param2>, ... )   DECLARE_DELEGATE_RetVal_<Num>Params( RetValType, DelegateName, Param1Type, Param2Type, ... )


Multi-cast与Dynamic声明绑定的方式与此类似
DECLARE_MULTICAST_DELEGATE...

DECLARE_DYNAMIC_DELEGATE...

DECLARE_DYNAMIC_MULTICAST_DELEGATE


将实际的函数绑定到Delegate的方式

Bind()
Binds to an existing delegate object.

BindStatic()
bind to a plain C++ class static function or global function.

BindRaw()
bind to a plain C++ class function, may be unsafe to call if the object was deleted. Be careful when calling Execute()!

BindSP()
Binds a shared pointer-based member function delegate.

BindUObject()
Binds a UObject-based member function delegate.

UnBind()
Unbinds this delegate.

执行Delegate的方式
Execute()
ExecuteIfBound()
IsBound()


Multi-cast Delegate

如果需要将多个方法绑定到同一个Delegate上,并且最终方法被一起调用, 则需要使用Multi-cast Delegate。

将实际的函数绑定到Delegate的方式

Add()
Adds a function delegate to this multi-cast delegate's invocation list.

AddStatic()  
Adds a raw C++ pointer global function delegate.

AddRaw()
Adds a raw C++ pointer delegate. Raw pointer does not use any sort of reference, so may be unsafe to call if the object was deleted out from underneath your delegate. Be careful when calling Execute()!

AddSP()
Adds a shared pointer-based (fast, not thread-safe) member function delegate. Shared pointer delegates keep a weak reference to your object.

AddUObject()
Adds a UObject-based member function delegate. UObject delegates keep a weak reference to your object.

Remove()
Removes a function from this multi-cast delegate's invocation list (performance is O(N)). Note that the order of the delegates may not be preserved!

RemoveAll()
Remove all functions from this multi-cast delegate's invocation list that are bound to the specified UserObject. Note that the order of the delegates may not be preserved!


执行Delegate的方式
Broadcast()  Broadcasts this delegate to all bound objects, except to those that may have expired.

Dynamic Delegate

如果需要从Blueprint中使用自定义事件,则必须使用Dynamic delegate,同时在delegate的实例上声明BlueprintAssignable,例如系统中的OnActorBeginOverlap事件:

DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorBeginOverlapSignature, AActor*, OverlappedActor, AActor*, OtherActor );

UPROPERTY(BlueprintAssignable, Category="Collision")
FActorBeginOverlapSignature OnActorBeginOverlap;

将实际的函数绑定到Delegate的方式

BindDynamic( UserObject, FuncName )
Helper macro for calling BindDynamic() on dynamic delegates. Automatically generates the function name string.

AddDynamic( UserObject, FuncName )    
Helper macro for calling AddDynamic() on dynamic multi-cast delegates. Automatically generates the function name string.

RemoveDynamic( UserObject, FuncName )
Helper macro for calling RemoveDynamic() on dynamic multi-cast delegates. Automatically generates the function name string.


执行Delegate的方式
Execute()
ExecuteIfBound()
IsBound()

下面举几个例子:
1.Single-cast Delegate

// 定义delegate
DECLARE_DELEGATE(FStandardDelegateSignature)

// 创建delegate的实例
FStandardDelegateSignature MyStandardDelegate;

// 需要绑定的函数
void ADelegateListener::EnableLight();

// 添加绑定
MyStandardDelegate.BindUObject(this, &ADelegateListener::EnableLight);

// 使用delegate。如果有函数绑定到delegate,则调用此函数
MyStandardDelegate.ExecuteIfBound();

// 解除绑定
MyStandardDelegate.Unbind();

2.Single-cast Delegate with OneParam

// 定义delegate,并说明此delegate需要传一个参数
DECLARE_DELEGATE_OneParam(FParamDelegateSignature, FLinearColor)

FParamDelegateSignature MyParameterDelegate;

void AParamDelegateListener::SetLightColor (FLinearColor LightColor);

MyParameterDelegate.BindUObject(this, &AParamDelegateListener::SetLightColor);

MyParameterDelegate.ExecuteIfBound(FLinearColor(1, 0, 0, 1));

3.Multi-cast Delegate

// 定义多播delegate
DECLARE_MULTICAST_DELEGATE(FMulticastDelegateSignature)

// 创建多播delegate的实例
FMulticastDelegateSignature MyMulticastDelegate;

// 需要添加到绑定中的函数
void AMulticastDelegateListener::ToggleLight();

FDelegateHandle MyDelegateHandle;

// 添加绑定,并返回此绑定的Handle
MyDelegateHandle = MyMulticastDelegate.AddUObject(this, &AMulticastDelegateListener::ToggleLight);

// 如果要解除绑定,则删除此绑定对应的Handle
MyMulticastDelegate.Remove(MyDelegateHandle);

// Broadcast()是ExecuteIfBound()的多播版本,不管是否添加了绑定,Broadcast()都可以被调用
MyMulticastDelegate.Broadcast();

4.Multi-cast Event with TwoParams

DECLARE_MULTICAST_DELEGATE_TwoParams(FOnTimeChangedSignature, int32, int32)

FOnTimeChangedSignature OnTimeChanged;

UPROPERTY()
int32 Hours;
UPROPERTY()
int32 Minutes;
UFUNCTION()
void TimeChanged(int32 Hours, int32 Minutes);

FDelegateHandle MyDelegateHandle;

MyDelegateHandle = OnTimeChanged.AddUObject(this, &AClock::TimeChanged);

OnTimeChanged.Broadcast(Hours, Minutes);

 

二、Event

Multi-cast Delegate Event

其与multi-cast delegate很类似,主要不同在于只有宣称事件的类才能调用事件的Broadcast, IsBound和Clear方法。

声明事件的方式

DECLARE_EVENT( OwningType, EventName )

DECLARE_EVENT_OneParam( OwningType, EventName, Param1Type )

DECLARE_EVENT_TwoParams( OwningType, EventName, Param1Type, Param2Type )

DECLARE_EVENT_<Num>Params( OwningType, EventName, Param1Type, Param2Type, ... )

将实际的函数绑定到Delegate的方式与Multi-cast Delegate相同

执行Delegate的方式
Broadcast()

举个例子:

// 第一个参数是实现了事件的类的类名,只有在这个类中才可调用Broadcast()。
// 第二个参数是事件名,它实际上是function signature。
DECLARE_EVENT(AMyTriggerVolume, FPlayerEntered)

// 实例化一个事件
FPlayerEntered OnPlayerEntered;

// 
UFUNCTION()
void OnTriggerEvent();

// 绑定函数到事件
OnPlayerEntered.AddUObject(this, &ATriggerVolEventListener::OnTriggerEvent);

// 通过调用 Broadcast()使用此事件
OnPlayerEntered.Broadcast();

Event Dispatchers

Event Dispatchers是在Blueprint中定义和使用Event的方式,

1.在Blueprint编辑器中创建一个Event Dispatcher(Creating Event Dispatchers)
2.在图表中添加Bind节点,将事件绑定到Event Dispatcher上(Binding Event To Event Dispatcher Node)
3.创建调用Event Dispatcher的节点(Calling Event Dispatcher Node)
4.创建需要使用Event Dispatcher的事件节点(Creating Event Dispatcher Event Node)
5.创建解绑节点(Creating Unbind or Unbind All Nodes)

UFUNCTION event

UFUNCTION事件实现了C++代码到Blueprint的调用,主要有如下两种方法:

UFUNCTION(BlueprintImplementableEvent, Category="Damage")
void CalledFromCpp()
UFUNCTION(BlueprintNativeEvent, Category="Damage")
void CalledFromCpp();

void AMyActor::CalledFromCpp_Implementation()
{
    // Do something cool here
}

声明为BlueprintImplementableEvent的UFUNCTION,C++中不用提供方法的定义,如果Blueprint中未使用此方法,则在C++调用此方法时什么也不做。
声明为BlueprintNativeEvent的UFUNCTION,C++中可以提供一个默认定义的方法,默认定义的方法名为在原方法名后加上_Implementation,如果Blueprint中未使用此方法,则在C++调用此默认定义的方法。

参考文档
Event Dispatchers1
Event Dispatchers2
Delegates & Events
Binding and declaring delegates in different ways

猜你喜欢

转载自blog.csdn.net/netyeaxi/article/details/81673691