Skip to content
Event

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

Event Class

event

The MW editor support multiple types of events.

When implementing logic, you can connect functions to built-in events triggered by the editor to respond to them. You can also create triggers and respond to customize events. In addition, you can use network events to allow event driven communication across client server boundaries.

Many objects have built-in events provided by their APIs that automatically respond to specific operations or changes related to these objects. For example, the weapon on equipment in HotWeaponwill trigger the onEquip event.

Event Classes provide events for communication between local, client, and server.

How is communication between local, client, and server?

  • Local: refers to the current host.

  • Server: The MW editor multiplayer game is based on the client server mode. That is to say, there will be a server serving as the main controller of the game state, while the connected clients will maintain an approximate replica. The server can be seen as a puppet teacher, and the client is the puppets control by the puppet teacher.

  • Client: One or more "puppets" connected to the server.

When communicating between the local, client, and server, it's like turning on a switch, and the lights in the room will light up. When I execute an event in a certain location (turn on the switch), the added event will trigger (the installed light bulb will light up).

Table of contents

Methods

addClientListener(eventName: string, listener: (player: Player, ...params: unknown[]) => void): EventListener other
The server listens to events sent by the client
addGameEventListener(eventName: string, callback: (data: string) => void): EventListener other
Register game level events
addLocalListener(eventName: string, listener: (...params: unknown[]) => void): EventListener other
Add local events.
addSceneEventListener(eventName: string, callback: (data: string) => void): EventListener other
Register scene level events, and you can receive the event broadcast in different rooms in the same scene
addServerListener(eventName: string, listener: (...params: unknown[]) => void): EventListener other
Client listens to server events
dispatchGameEvent(eventName: string, data: string): void other
Broadcast game level events, which can be received in different rooms within the same game
dispatchSceneEvent(eventName: string, data: string): void other
Broadcast scene level events can be received in different rooms within the same scene
dispatchToAllClient(eventName: string, ...params: unknown[]): DispatchEventResult other
The server sends events to all clients
dispatchToAllClientUnreliable(eventName: string, ...params: unknown[]): DispatchEventResult other
The server sends unreliable events to all clients, and there is no retransmission mechanism for unreliable events. They will be lost when encountering network fluctuations or other situations
dispatchToClient(player: Player, eventName: string, ...params: unknown[]): DispatchEventResult other
The server sends events to the specified client
dispatchToClientUnreliable(player: Player, eventName: string, ...params: unknown[]): DispatchEventResult other
The server sends unreliable events to the specified client, and there is no retransmission mechanism for unreliable events. When encountering network fluctuations or other situations, they will be lost
dispatchToLocal(eventName: string, ...params: unknown[]): DispatchEventResult other
Execute the added local event.
dispatchToServer(eventName: string, ...params: unknown[]): DispatchEventResult other
The client sends events to the server
dispatchToServerUnreliable(eventName: string, ...params: unknown[]): DispatchEventResult other
The client sends unreliable events to the server, but there is no mechanism to resend unreliable events. When encountering network fluctuations or other situations, they will be lost
removeListener(event: EventListener): void other
Remove event listener

Methods

addClientListener

Static addClientListener(eventName, listener): EventListener other

The server listens to events sent by the client

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but please set an appropriate length and name.
listener (player: Player, ...params: unknown[]) => voidUsage: Listen to the client target event content of the callback Player sending events

Returns

EventListenerReturn an event listener

Precautions

Should be used in the server-side logic

Usage example: create a script named "EventSample", place it in the object manager, open the script, enter the following code to save it, run the game, and you will see the print ok of each frame in the server. The code is as follows:

ts
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // The client sends an eventOne event to the server
         // The client sending the eventOne event can be seen as a light switch
         if(SystemUtil.isClient()){
             Event.dispatchToServer("eventOne");
         }
         // Execute the eventOne event sent by the client on the server and execute the incoming function logic on the server
         // The server executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if (SystemUtil.isServer()){
             Event.addClientListener("eventOne" ,()=>{console.log("ok")});
         }
     }
 }
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // The client sends an eventOne event to the server
         // The client sending the eventOne event can be seen as a light switch
         if(SystemUtil.isClient()){
             Event.dispatchToServer("eventOne");
         }
         // Execute the eventOne event sent by the client on the server and execute the incoming function logic on the server
         // The server executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if (SystemUtil.isServer()){
             Event.addClientListener("eventOne" ,()=>{console.log("ok")});
         }
     }
 }

addGameEventListener

Static addGameEventListener(eventName, callback): EventListener other

Register game level events

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set an appropriate length and name.
callback (data: string) => voidUsage: The callback triggered when receiving a registered event

Returns

EventListener

addLocalListener

Static addLocalListener(eventName, listener): EventListener other

Add local events.

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but please set an appropriate length and name.
listener (...params: unknown[]) => voidUsage: Listening for callbacks

Returns

EventListenerReturn an event listener

addSceneEventListener

Static addSceneEventListener(eventName, callback): EventListener other

Register scene level events, and you can receive the event broadcast in different rooms in the same scene

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set an appropriate length and name.
callback (data: string) => voidUsage: The callback triggered when receiving a registered event

Returns

EventListener

addServerListener

Static addServerListener(eventName, listener): EventListener other

Client listens to server events

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set appropriate length and name.
listener (...params: unknown[]) => voidUsage: Listen for callback params event content

Returns

EventListenerReturn an event listener

Precautions

Should be called in the client logic

Usage example: create a script named "EventSample", place it in the object manager, open the script, enter the following code to save it, run the game, and you will see the print ok of each frame in the client. The code is as follows:

ts
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // Execute the eventOne event sent by the server on the client side, and execute the incoming function logic on the client side
         // The client executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if(SystemUtil.isClient()){
             Event.addServerListener("eventOne",()=>{console.log("ok")});
         }
     }
     protected onUpdate(dt: number): void {
         // The server sends an eventOne event to all clients every frame
         // The server sending the eventOne event can be seen as a light switch, with the light bulb being turned on and off once per frame
         if (SystemUtil.isServer()){
             Event.dispatchToAllClient("eventOne");
         }
     }
 }
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // Execute the eventOne event sent by the server on the client side, and execute the incoming function logic on the client side
         // The client executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if(SystemUtil.isClient()){
             Event.addServerListener("eventOne",()=>{console.log("ok")});
         }
     }
     protected onUpdate(dt: number): void {
         // The server sends an eventOne event to all clients every frame
         // The server sending the eventOne event can be seen as a light switch, with the light bulb being turned on and off once per frame
         if (SystemUtil.isServer()){
             Event.dispatchToAllClient("eventOne");
         }
     }
 }

dispatchGameEvent

Static dispatchGameEvent(eventName, data): void other

Broadcast game level events, which can be received in different rooms within the same game

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set an appropriate length and name.
data stringUsage: Carrying data
Range: No length limit.

dispatchSceneEvent

Static dispatchSceneEvent(eventName, data): void other

Broadcast scene level events can be received in different rooms within the same scene

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set an appropriate length and name.
data stringUsage: Carrying data
Range: No length limit.

dispatchToAllClient

Static dispatchToAllClient(eventName, ...params): DispatchEventResult other

The server sends events to all clients

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set appropriate length and name.
...params unknown[]Usage: Variable length parameter

Returns

DispatchEventResultReturn the event and send the result

Precautions

Call in server logic

Usage example: create a script named "EventSample", place it in the object manager, open the script, enter the following code to save it, run the game, and you will see the print ok of each frame in the client. The code is as follows:

ts
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // Execute the eventOne event sent by the server on the client side, and execute the incoming function logic on the client side
         // The client executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if(SystemUtil.isClient()){
             Event.addServerListener("eventOne",()=>{console.log("ok")});
         }
     }
     protected onUpdate(dt: number): void {
         // The server sends an eventOne event to all clients every frame
         // The server sending the eventOne event can be seen as a light switch, with the light bulb being turned on and off once per frame
         if (SystemUtil.isServer()){
             Event.dispatchToAllClient("eventOne");
         }
     }
 }
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // Execute the eventOne event sent by the server on the client side, and execute the incoming function logic on the client side
         // The client executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if(SystemUtil.isClient()){
             Event.addServerListener("eventOne",()=>{console.log("ok")});
         }
     }
     protected onUpdate(dt: number): void {
         // The server sends an eventOne event to all clients every frame
         // The server sending the eventOne event can be seen as a light switch, with the light bulb being turned on and off once per frame
         if (SystemUtil.isServer()){
             Event.dispatchToAllClient("eventOne");
         }
     }
 }

dispatchToAllClientUnreliable

Static dispatchToAllClientUnreliable(eventName, ...params): DispatchEventResult other

The server sends unreliable events to all clients, and there is no retransmission mechanism for unreliable events. They will be lost when encountering network fluctuations or other situations

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set appropriate length and name.
...params unknown[]Usage: Variable length parameter

Returns

DispatchEventResultReturn the event and send the result

Precautions

Call in server logic

Usage example: create a script named "EventSample", place it in the object manager, open the script, enter the following code to save it, run the game, and you will see the print ok of each frame in the client. The code is as follows:

ts
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // Execute the eventOne event sent by the server on the client side, and execute the incoming function logic on the client side
         // The client executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if(SystemUtil.isClient()){
             Event.addServerListener("eventOne",()=>{console.log("ok")});
         }
     }
     protected onUpdate(dt: number): void {
         // The server sends an eventOne event to all clients every frame
         // The server sending the eventOne event can be seen as a light switch, with the light bulb being turned on and off once per frame
         if (SystemUtil.isServer()){
             Event.dispatchToAllClientUnreliable("eventOne");
         }
     }
 }
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // Execute the eventOne event sent by the server on the client side, and execute the incoming function logic on the client side
         // The client executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if(SystemUtil.isClient()){
             Event.addServerListener("eventOne",()=>{console.log("ok")});
         }
     }
     protected onUpdate(dt: number): void {
         // The server sends an eventOne event to all clients every frame
         // The server sending the eventOne event can be seen as a light switch, with the light bulb being turned on and off once per frame
         if (SystemUtil.isServer()){
             Event.dispatchToAllClientUnreliable("eventOne");
         }
     }
 }

dispatchToClient

Static dispatchToClient(player, eventName, ...params): DispatchEventResult other

The server sends events to the specified client

Parameters

player PlayerUsage: Client
eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set appropriate length and name.
...params unknown[]Usage: Variable length parameter

Returns

DispatchEventResultReturn the event and send the result

Precautions

Call in server logic


dispatchToClientUnreliable

Static dispatchToClientUnreliable(player, eventName, ...params): DispatchEventResult other

The server sends unreliable events to the specified client, and there is no retransmission mechanism for unreliable events. When encountering network fluctuations or other situations, they will be lost

Parameters

player PlayerUsage: Client
eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set appropriate length and name.
...params unknown[]Usage: Variable length parameter

Returns

DispatchEventResultReturn the event and send the result

Precautions

Call in server logic


dispatchToLocal

Static dispatchToLocal(eventName, ...params): DispatchEventResult other

Execute the added local event.

Parameters

eventName stringUsage: Event name
Range: The length is not limited and is paired with the added event name.
...params unknown[]Usage: Event content

Returns

DispatchEventResultReturn the result of sending local events

dispatchToServer

Static dispatchToServer(eventName, ...params): DispatchEventResult other

The client sends events to the server

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set appropriate length and name.
...params unknown[]Usage: Variable length parameter

Returns

DispatchEventResultReturn the event and send the result

Precautions

Should be called in the client logic

Usage example: create a script named "EventSample", place it in the object manager, open the script, enter the following code to save it, run the game, and you will see the print ok of each frame in the server. The code is as follows:

ts
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // The client sends an eventOne event to the server
         // The client sending the eventOne event can be seen as a light switch
         if(SystemUtil.isClient()){
             Event.dispatchToServer("eventOne");
         }
         // Execute the eventOne event sent by the client on the server and execute the incoming function logic on the server
         // The server executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if (SystemUtil.isServer()){
             Event.addClientListener("eventOne" ,()=>{console.log("ok")});
         }
     }
 }
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // The client sends an eventOne event to the server
         // The client sending the eventOne event can be seen as a light switch
         if(SystemUtil.isClient()){
             Event.dispatchToServer("eventOne");
         }
         // Execute the eventOne event sent by the client on the server and execute the incoming function logic on the server
         // The server executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if (SystemUtil.isServer()){
             Event.addClientListener("eventOne" ,()=>{console.log("ok")});
         }
     }
 }

dispatchToServerUnreliable

Static dispatchToServerUnreliable(eventName, ...params): DispatchEventResult other

The client sends unreliable events to the server, but there is no mechanism to resend unreliable events. When encountering network fluctuations or other situations, they will be lost

Parameters

eventName stringUsage: Event Name
Range: There is no length limit, but it is recommended to set appropriate length and name.
...params unknown[]Usage: Variable length parameter

Returns

DispatchEventResultReturn the event and send the result

Precautions

Should be called in the client logic

Usage example: create a script named "EventSample", place it in the object manager, open the script, enter the following code to save it, run the game, and you will see the print ok of each frame in the server. The code is as follows:

ts
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // The client sends an eventOne event to the server
         // The client sending the eventOne event can be seen as a light switch
         if(SystemUtil.isClient()){
             Event.dispatchToServerUnreliable("eventOne");
         }
         // Execute the eventOne event sent by the client on the server and execute the incoming function logic on the server
         // The server executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if (SystemUtil.isServer()){
             Event.addClientListener("eventOne" ,()=>{console.log("ok")});
         }
     }
 }
@Component
 export default class EventSample extends Script {
     protected async onStart(): Promise<void> {
         this.useUpdate = true;
         // The client sends an eventOne event to the server
         // The client sending the eventOne event can be seen as a light switch
         if(SystemUtil.isClient()){
             Event.dispatchToServerUnreliable("eventOne");
         }
         // Execute the eventOne event sent by the client on the server and execute the incoming function logic on the server
         // The server executes the eventOne event, and the incoming function starts executing, which can be seen as a light bulb turning on
         if (SystemUtil.isServer()){
             Event.addClientListener("eventOne" ,()=>{console.log("ok")});
         }
     }
 }

removeListener

Static removeListener(event): void other

Remove event listener

Parameters

event EventListenerUsage: listener