UE4 commission

01 entrust (agent)

A delegation, also known as a proxy, is essentially an object of a special class, which can store (one or more) function pointers, call parameters, and return values.

A delegate acts like a function pointer, but it is safer (supports compile-time type checking) and is easier to use.

Using a delegate, the function of the object can be called through an execution of the delegate.

02 Concept

Agent : When you don’t know what the object is, you can also execute the function in the object. The delegate agent establishes communication between the two objects. When I need to call the members of the object, we don’t know who the object is. At this time, you only need to call the intermediary delegate. The commissioned function is similar to the transmitter in the blueprint.

The agent has three main characteristics: binding, calling, and unbinding.

03 UE4 supports proxy types

  1. Unicast proxy: support binding a single function
  2. Multicast proxy: support binding multiple functions
  3. Dynamic proxy: serializable Uobject

04 Use of Unicast Proxy

There are four main types of agents:
  • Without formal parameters, without return value
  • With formal parameters, without return value
  • With return value, without formal parameters
  • With return value, with formal parameters
Agent creation method:

Unreal 4 has its own set of delegates, which need to be created through delegate macros.

//声明委托 不带形参,不带返回值(它可以绑定的这个函数必须是没有参数也没有返回值的) 
DECLARE_DELEGATE(FDelegateTestA);
//声明委托 带形参,不带返回值,类似的一共有9个宏,最大带9个形参 如:DECLARE_DELEGATE_NineParams
DECLARE_DELEGATE_OneParam(FDelegateTestB,bool);
//声明委托带 带返回值,不带形参
DECLARE_DELEGATE_RetVal(bool,FDelegateTestC);
//声明委托 带返回值,带形参的,类似的一共有9个宏,最大带9个形参 如:DECLARE_DELEGATE_NineParams
DECLARE_DELEGATE_RetVal_OneParam(int32,FDelegateTestD,FString&);

Currently Unreal 4 macros can take up to 9 formal parameters, such as: DECLARE_DELEGATE_OneParam all the way to DECLARE_DELEGATE_NineParams

Unicast proxy case: proxy without parameters, binding C++ native class

Bind the UE4 object, followed by the bound function

First declare two classes and bind the proxy

#include "CoreMinimal.h"
//声明委托
DECLARE_DELEGATE(FDelegateTestA);

//声明ClassA类
class ClassA 
{
    
    
public:
//声明代理
	FDelegateTestA DelegateTestA;
  
//调用代理
	void Eexcute()
	{
    
    
		//调用代理,先判断是否绑定了,绑定上了再执行代理。
		DelegateTestA.ExecuteIfBound();
	}
};

//声明ClassB类
class ClassB 
{
    
    
public:
	//构造函数,绑定代理
	ClassB(ClassA* NewClass_a)
	if (NewClass_a)
	{
    
    
		m_classA = NewClass_a;
		m_classA->DelegateTestA.BindRaw(this, &ClassB::ExecuteA);//绑定事件ExecuteA
        }
	void ExecuteA()
	{
    
    
		Print_F(TEXT("Hello world"));
	}
	//析构函数,解除绑定
	~ClassB()
	{
    
    
		if (m_classA)
		{
    
    
			m_classA->DelegateTestA.Unbind();
			m_classA->Execute();
			delete m_classA;
			m_classA = nullptr;
                 }
private:
	ClassA* m_classA;
};

Call agent

//假设为程序入口
void BeginPlay()
{
    
    
	//创建ClassA指针
	ClassA* NewClassA = new ClassA();
	//创建ClassB指针,构建函数
	ClassB* NewClassB = new ClassB(NewClassA);
	//调用代理Execute(),Execute会调用ClassB中的Execute()A函数
        //NewClassA不知道有对象NewClassB的,存在却可以调用NewClassB的成员
        //代理为ClassA类和ClassB之间建立通信
	NewClassA->Execute();
	//释放指
	delete NewClassB;
	NewClassA = nullptr;
	NewClassB = nullptr;
}

result

//屏幕会打印“Hello world”,ClassA在不知道对象ClassB情况下调用了ClassB的成员

05 Use of dynamic proxy

Dynamic delegates can be serialized, and their functions can be searched by name, but their execution speed is slower than regular delegates.

statement
DECLARE_DYNAMIC_DELEGATE[
Bind
Auxiliary macro Description
BindDynamic( UserObject, FuncName ) Auxiliary macro used to call BindDynamic() on dynamic delegates. Automatically generate function named strings.
carried out
Execution function description
Execute Perform a delegate without checking its binding
ExecuteIfBound Check whether a delegate is bound, if so, call Execute
IsBound Check if a delegate is bound, often containing Executethe code before calling

06 Use of Multicast Proxy

Multicast delegation has most of the same functions as unicast delegation. They only have weak references to objects, can be used with structures, can be easily copied around, etc.
Just like regular delegates, multicast delegates can be loaded/saved and triggered remotely; however, multicast delegate functions cannot use return values. They are best used to easily pass a group of delegates around.

Event is a special type of multicast delegate, it will be restricted when accessing the "Broadcast()", "IsBound()" and "Clear()" functions.

statement
Declaration macro Description
DECLARE_MULTICAST_DELEGATE[_RetVal, ...]( DelegateName ) Create a multicast delegate.
DECLARE_DYNAMIC_MULTICAST_DELEGATE[_RetVal, ...]( DelegateName ) Create a dynamic multicast delegate.
Bind

Multicast delegates can bind multiple functions, and all these functions will be called when the delegate is triggered. Therefore, the binding function is more similar to the array in semantics.

function Description
“Add()” Add the function delegate to the call list of the multicast delegate.
“AddStatic()” Add the original C++ pointer global function delegate.
“AddRaw()” Add raw C++ pointer delegate. The raw pointer does not use any kind of reference, so if the object is deleted from the delegate, it may not be safe to call this function. Please be careful when calling Execute()!
“AddSP()” Add (fast, non-thread-safe) member function delegation based on shared pointers. The shared pointer delegate retains a weak reference to the object.
“AddUObject()” Add a member function delegate based on UObject. The UObject delegate retains a weak reference to the object.
“Remove()” Remove the function from the call list of the multicast delegate (performance is O(N)). Please note that the order of delegation may not be preserved!
“RemoveAll()” Remove all functions bound to the specified UserObject from the call list of the multicast delegate. Please note that the order of delegation may not be preserved!

"RemoveAll()" will delete all registered delegates bound to the provided pointer. Remember, the original delegate that is not bound to the object pointer will not be deleted by this function!

carried out

Multicast delegates allow you to attach multiple function delegates, and then execute them at the same time by calling the "Broadcast()" function of the multicast delegate. Multicast delegated signatures must not use the return value.

It is always safe to call "Broadcast()" on a multicast delegate, even when there is no binding. The only thing to note is that if you use delegates to initialize output variables, it usually brings very unfavorable consequences.

The execution order of the bound functions when calling "Broadcast()" has not been defined. The order of execution may be different from the order in which functions are added.

function Description
“Broadcast()” Broadcast the delegate to all bound objects, except for objects that may have expired.

Guess you like

Origin blog.csdn.net/qq_41363459/article/details/111466285