Skip to content
Delegate<T>

[Basic Type](../groups/Basic Type.Basic Type.md) / Delegate

Delegate<T> Class

entrust


Delegate represents a set of specific tasks, and can call the specified function to complete these tasks. Delegate provides a flexible way to assign tasks to different functions, and dynamically add or delete tasks at runtime.

The editor has packaged delegation functions that can be used directly for you, which are divided into three types of delegation:

  1. Delegate A unicast delegate refers to a delegate that can only bind one function and achieve one-to-one notification.

  2. MulticastDelegate Multicast delegation refers to a delegation that can bind multiple functions to achieve one to many notifications.

  3. MulticastGameObjectDelegate Multicast dragging associated with GameObject allows one to many notifications.

How to understand delegate?

One kind of understanding that hits the soul directly is that it turns a function into something identical and can be saved with variables.

The common way is through events, such as placing a callback in the input event.

Functions are different from objects. Objects can be instantiated to copy many different entities in memory. After the program starts, functions only have one copy in memory. The implementation method of non static functions is to add a 'this' as the first parameter and pass it in, thus achieving the binding relationship.

The achieve scheme of Delegate is to create an object to link the address of a function, which can be a single function address or a function address set. When call, these functions will be called. If the linking process involves function objects, the instance of the object will be stored in Delegate.

What is the difference between delegate and agency?

  • Proxy is a method used to solve problems. Providing a 'control over one class over another' is a relationship between classes.

  • Delegation is a type of application method. Delegate provides that "the execution of a method will execute the method loaded on it at the same time". It is the relationship between methods.

  • Delegate may replace agency, but agency cannot replace delegate.

  • Delegation can dynamically load methods, but proxies cannot implement them.

Effect

The calling end takes effect

Example usage: Create a script named "DelExample", open the script, enter the following code to save, run the game, and print output 5.

ts
@Component
 export default class DelExample extends Script {

     // Example function that satisfies the DelegateFuncType constraint
     public addNumbers(a: number, b: number): number {
         return a + b;
     }
     protected onStart(): void {

         // Instantiate Delegate class
         const delegateInstance = new Delegate<typeof this.addNumbers>();
         // Method for binding Delegate instances
         delegateInstance.bind(this.addNumbers);
         // Call the method of Delegate instance
         const result = delegateInstance.execute(2,3);
         // Output: 5
         console.log(result);
     }
 }
@Component
 export default class DelExample extends Script {

     // Example function that satisfies the DelegateFuncType constraint
     public addNumbers(a: number, b: number): number {
         return a + b;
     }
     protected onStart(): void {

         // Instantiate Delegate class
         const delegateInstance = new Delegate<typeof this.addNumbers>();
         // Method for binding Delegate instances
         delegateInstance.bind(this.addNumbers);
         // Call the method of Delegate instance
         const result = delegateInstance.execute(2,3);
         // Output: 5
         console.log(result);
     }
 }

Type parameters

Textends DelegateFuncType

Implements

  • DelegateInterface<T>

Table of contents

Methods

bind(func: T): void other
Binding event
execute(...arg: Parameters<T>): ReturnType<T> other
Execute binding event
isBound(): boolean other
Is the event bound
unbind(): void other
Unbinding event

Type parameters

Textends DelegateFuncType

Methods

bind

bind(func): void other

Binding event

Parameters

func TBinding function default:

Implementation of

DelegateInterface.bind


execute

execute(...arg): ReturnType<T> other

Execute binding event

Parameters

...arg Parameters<T>Execution parameters

Returns

ReturnType<T>DelegateInterface.execute

isBound

isBound(): boolean other

Is the event bound

Returns

booleanDelegateInterface.isBound

unbind

unbind(): void other

Unbinding event

Implementation of

DelegateInterface.unbind