Network events

A network event runs OnNetworkEvent of the same script, on the same object, on other games: "the bell rang", "this player scored". It is a moment, not a state: a player who arrives later does not get the events sent before. What must last (a score, a door left open) goes in the synced state.

csharp
using NodeWorld.Flux;

namespace MyWorld
{
    public class Bell : FluxScript
    {
        public AudioSource ring;

        protected override void Start() => Interaction.SetInteractable(gameObject, true);

        protected override void OnInteract()
        {
            var arguments = new NetWriter();
            arguments.Write(Player.name);
            SendNetworkEvent(NetworkEventTarget.All, "Ring", arguments);
        }

        protected override void OnNetworkEvent(string name, NetReader arguments, int senderId)
        {
            if (name != "Ring") return;
            Debug.Log(arguments.ReadString() + " rang the bell");
            ring.Play();
        }
    }
}

Who runs it

SendNetworkEvent(target, …)Runs on
NetworkEventTarget.Allevery player of the room: the sender at once, the others when it arrives
NetworkEventTarget.Othersevery player but the sender
NetworkEventTarget.Ownerthe owner of the object (at once if it is the sender)
NetworkEventTarget.Masterthe master of the room (at once if it is the sender)
NetworkEventTarget.Serverthe same script on the server only (below)

SendNetworkEventToPlayer(playerId, name, arguments) runs it on one player (at once if it is the local player).

OnNetworkEvent(name, arguments, senderId) gets the name, the arguments to read, and the id of the player who sent it.

Arguments

Write them with a NetWriter and read them from the NetReader in the same order (the types are those of the synced state). Without arguments, the reader is empty.

Rules

  • The object needs a Network Id on its FL# Script component. Without one, the event stays on this player's game and the log says so once: 'Bell' (MyWorld.Bell) sends network events but has no network id: they stay on this client.
  • A name has 1 to 128 characters; the arguments 16 KB at most.
  • Each player sends 120 messages a second at most to the network of the scripts (synced states, events and spawned objects together); the server drops the rest.

Ask the server

NetworkEventTarget.Server runs OnNetworkEvent of the same script on the same object on the server, with the id of the player who sent it. The script must run there (not [ClientOnly]). It is how a player's script asks for what only the server's scripts do, such as opening a portal.

csharp
// On the player's game:
protected override void OnInteract() => SendNetworkEvent(NetworkEventTarget.Server, "Open");

// On the server:
protected override void OnNetworkEvent(string name, NetReader arguments, int senderId)
{
    if (!World.isServer || name != "Open") return;
    Debug.Log("Player " + senderId + " asked to open it");
}

The server's scripts send no event to the players: an event they send to NetworkEventTarget.Server stays on the server.