Skip to main content

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:

EmitterReceiverAction
Client emitToPlayersClient onPlayerEventEmits to all players
Client emitToPlayerClient onPlayerEventEmits to a specific player
Client emitToServerServer onPlayerEventEmits to the server
Client emitToPlayersWithAccessClient onPlayerEventEmits to all players with the specified access
Client emitToPlayersWithRolesClient onPlayerEventEmits to all players with the specified roles
Client emitToScriptsClient onScriptEventOnly emits to local scripts
Server emitToPlayersClient onServerEventEmits to all players
Server emitToPlayerClient onServerEventEmits to a specific player
Server emitToPlayersWithAccessClient onServerEventEmits to all players with the specified access
Server emitToPlayersWithRolesClient onServerEventEmits to all players with the specified roles
Server emitToScriptsClient Server onScriptEventEmits to all scripts, including client scripts
SECURITY NOTE

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.