Networking
The Networking
feature of the engine allows different parts of the script to communicate with each other by emitting and listening to events. It's possible to synchronize data, send messages, and trigger actions across the server, client, and scripts.
In this section, we'll provide an overview of the Networking
feature and explain its purpose in the script.
Events
Custom events can be defined using the engine.network.*
methods like engine.network.emitToPlayers
, providing a flexible way to extend the functionality of the Networking feature. With custom events, developers can define their own event types, data, and listeners, which can be triggered and received by other parts of the script. This allows for a wide range of possibilities, from syncing player inventory and stats to triggering custom animations and effects. Custom events provide a way to tailor the Networking feature to meet the specific needs of your project and create a unique experience for players.
See the full Methods List.
Emitting and Listening to Events
To emit a custom event, you can use one of the engine.network.*
methods listed in the previous section and specify the event name and data. The data can be any valid JSON data, such as a string, number, or object.
Here's an example of defining, emitting, and listening to a custom event:
// Emit a custom event
engine.emitToPlayers('myCustomEvent', {message: 'Hello, players!'});
// Listen for the custom event and log the message
engine.onPlayerEvent('myCustomEvent', data => {
console.log('Received message: ' + data.message);
});
Event Emitters and Receivers
Based on where the event is emitted from, the event can be received by different listeners. This flexibility in communication channels allows for a wide range of possibilities in creating custom experiences for your project.
Here's a table of all the possible combinations of event emitters and receivers:
Emitter | Receiver | Action |
---|---|---|
Client emitToPlayers | Client onPlayerEvent | Emits to all players |
Client emitToPlayer | Client onPlayerEvent | Emits to a specific player |
Client emitToServer | Server onPlayerEvent | Emits to the server |
Client emitToPlayersWithAccess | Client onPlayerEvent | Emits to all players with the specified access |
Client emitToPlayersWithRoles | Client onPlayerEvent | Emits to all players with the specified roles |
Client emitToScripts | Client onScriptEvent | Only emits to local scripts |
Server emitToPlayers | Client onServerEvent | Emits to all players |
Server emitToPlayer | Client onServerEvent | Emits to a specific player |
Server emitToPlayersWithAccess | Client onServerEvent | Emits to all players with the specified access |
Server emitToPlayersWithRoles | Client onServerEvent | Emits to all players with the specified roles |
Server emitToScripts | Client Server onScriptEvent | Emits to all scripts, including client scripts |
It's important to note that with this flexibility comes the responsibility to ensure that events are emitted and received securely. Developers should take care to validate the data sent with events and ensure that sensitive information is not exposed to unintended listeners as well as ensure that events are only emitted by authorized sources.