Portals made by scripts

Since SDK 0.2.1

A world's portal leads every player of the room to another world. Only the world's scripts on the server make portals, with Portals.Spawn, Portals.Update and Portals.Despawn: on a player's device these calls return false. A player's script that wants a portal sends a network event to the same script on the server, which opens it.

Open a portal on the server

csharp
using NodeWorld.Flux;

public class HomePortal : FluxScript
{
    public string targetServer = "";       // id of the server of the target world
    public string targetWorld = "HomeWorld";

    protected override void Start()
    {
        if (!World.isServer) return;
        Portals.Spawn("home", new PortalOptions
        {
            targetServer = targetServer,
            targetWorld = targetWorld,
            instanceCode = "",               // empty: the central API picks the instance
            label = "Home",                  // empty: the name of the target world
            position = transform.position,
            rotation = transform.rotation,
        });
    }

    protected override void OnDestroy()
    {
        if (World.isServer) Portals.Despawn("home");
    }
}
  • The portal id ("home") is the world's own, unique in the room. Spawn returns false if the server refused it (an empty target, an id already used).
  • Portals.Update moves it or changes where it leads; Portals.Despawn closes it.
  • The portals of a world do not expire; they close with the room, or when the script closes them.

A portal asked by a player

Send a network event to the server with NetworkEventTarget.Server: it runs OnNetworkEvent of the same script, on the same object, on the server, with the id of the player who sent it. The object needs a network ID (its FL# Script component), and the script must run on the server (not [ClientOnly]).

csharp
public class PortalKiosk : FluxScript
{
    public string targetServer = "";
    public string targetWorld = "HomeWorld";

    protected override void Start()
    {
        if (!World.isServer) Interaction.SetInteractable(gameObject, true);
    }

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

    // On the server: opens the portal in front of the kiosk.
    protected override void OnNetworkEvent(string name, NetReader arguments, int senderId)
    {
        if (!World.isServer || name != "Open") return;
        Portals.Spawn("kiosk", new PortalOptions
        {
            targetServer = targetServer,
            targetWorld = targetWorld,
            position = transform.position + transform.forward * 2f,
            rotation = transform.rotation,
        });
    }
}

An event to NetworkEventTarget.Server reaches the server only: the player who sends it does not run it, nor do the other players. It counts in the 120 messages a second each player may send to the network of the scripts.