Network Packets

This script shows how to use network packets:

// Listen to a player event, in this case item right-click
events.listen('item.right_click', function (event) {
  // Check if item was right-clicked on client or server side
  if (event.player.isServer()) {
    // Send data {test: 123} to channel "test_channel_1". Channel ID can be any string, but it's recommended to keep it to snake_case [a-z_0-9].
    // Receiving side will either be client (because its fired from server).
    event.player.sendData('test_channel_1', {
      test: 123
    })
  } else {
    // It's not required to use a different channel ID, but it's recommended.
    // Receiving side will either be server (because its fired from client).
    event.player.sendData('test_channel_2', {
      test: 456
    })
  }
})

// Listen to event that gets fired when network packet is received from server.
events.listen('player.data_from_server.test_channel_1', function (event) {
  log.info(event.data.get('test').asInt()) // Prints 123
})

// Listen to event that gets fired when network packet is received from client.
events.listen('player.data_from_client.test_channel_2', function (event) {
  log.info(event.data.get('test').asInt()) // Prints 456
})

Revision #1
Created Sun, Jun 28, 2020 5:17 PM by LatvianModder
Updated Sun, Jun 28, 2020 5:18 PM by LatvianModder