INetwork
namespace Griff.Runtime;
public interface INetwork : IGameServiceMultiplayer, as a service of the game (Game.Network, or Game.Services.Get<INetwork>()): hosting a game, joining one, who’s playing, and messages between machines. A game has a network session (Session) only while it’s networked: from Host or Join until it ends. Everything here answers offline too (no players, everything this machine’s, a message or command just runs here), so a system or a script asks without checking first.
The host runs the game. Everyone else’s machine shows the host’s world, sends the host its player’s keys, and moves its player’s own character itself so it never waits on the connection. Cameras, the HUD, menus and music stay on each machine. Put a Multiplayer component in the scene to give each player a character and choose teams.
Properties
Section titled “Properties”FakeLag
Section titled “FakeLag”public abstract NetConditions FakeLag { get; set; }Pretend network trouble (lag, lost packets) on what this machine sends, to try the game on a bad connection.
IsClient
Section titled “IsClient”public abstract bool IsClient { get; }True when playing in a game someone else hosts.
IsConnecting
Section titled “IsConnecting”public abstract bool IsConnecting { get; }True from Join until the host lets this machine in (or doesn’t).
IsHost
Section titled “IsHost”public abstract bool IsHost { get; }True on the machine running a network game: a host, or a dedicated server.
IsOnline
Section titled “IsOnline”public abstract bool IsOnline { get; }True while hosting or playing in someone else’s game.
LocalPlayer
Section titled “LocalPlayer”public abstract NetPlayer? LocalPlayer { get; }The player on this machine (null offline, and on a dedicated server).
OverNetwork
Section titled “OverNetwork”public abstract bool OverNetwork { get; set; }Whether this game’s network play reaches other machines. Off, as a game starts, it stays on this one: the sockets it hosts and joins on are reachable only from here (so Windows never asks about the firewall) and Status gives 127.0.0.1 as the address to join. The host’s to choose, for this game alone: the player and the editor’s Play turn it on for the game they run; a test, a simulation or a tool leaves it off, so one that hosts never opens a port to the network.
PlayerName
Section titled “PlayerName”public abstract string PlayerName { get; set; }The name others see, remembered in the save data. A TextField for the player’s name sets it.
Players
Section titled “Players”public abstract IReadOnlyList<NetPlayer> Players { get; }Everyone in the game, the host first (none offline).
RemotePlayers
Section titled “RemotePlayers”public abstract IReadOnlyList<NetPlayer> RemotePlayers { get; }On the host, the other players whose keys arrive from their machines, each with their controls (Input): what runs for each player is handed the player and their input. None elsewhere.
public abstract NetRole Role { get; }What this machine is in a network game: Offline without a session.
Sender
Section titled “Sender”public abstract NetPlayer? Sender { get; }Who sent the message being handled right now (inside a listener for a message from another machine).
Session
Section titled “Session”public abstract NetSession? Session { get; }The network session while this game is networked (hosting, joining or joined), or null offline.
Settings
Section titled “Settings”public abstract Multiplayer? Settings { get; }The scene’s Multiplayer settings, if it has them.
Status
Section titled “Status”public abstract string Status { get; }A line about how it’s going, for a menu: “Hosting on 192.168.1.5:7777”, “Joining…”, why it ended (kept after).
Transport
Section titled “Transport”public abstract INetTransport Transport { get; set; }What hosting and joining open their sockets with and find a typed address by: UDP (UdpTransport) unless the host hands another (Transport), so a platform with its own way to reach other machines (a browser’s) is one more INetTransport.
Unavailable
Section titled “Unavailable”public abstract string? Unavailable { get; set; }Why this machine can’t play over the network, or null when it can. In a browser (the web player) it’s BrowserUnavailable: a browser has no UDP, so hosting and joining put this in Status (and joining raises Ended with it) rather than failing some other way. Playing on sockets the caller makes is still allowed.
Methods
Section titled “Methods”AddScore
Section titled “AddScore”public abstract void AddScore(NetPlayer player, int points)Adds points to a player’s score (on the host; everyone sees it). Nothing elsewhere.
public abstract bool Host(int port = 0)Hosts a game on port (the Multiplayer’s, or DefaultPort) that others can join from anywhere that can reach this machine. Returns false, with why in Status, if it can’t.
public abstract void Host(INetSocket socket, bool dedicated = false)Hosts on a socket of the caller’s choosing, e.g. one on a MemoryNetwork.
HostServer
Section titled “HostServer”public abstract bool HostServer(int port = 0)Like Host, but this machine only runs the game and doesn’t play: a dedicated server.
IsMine
Section titled “IsMine”public abstract bool IsMine(Entity? entity)True if entity belongs to the player on this machine. Offline, true for everything.
public abstract void Join(string? address, int port = 0)Joins the game hosted at address: “192.168.1.5”, “my-pc”, or “my-pc:7777”. Empty (or “lan”) looks for a game on the local network and joins the first one found. Watch IsConnecting and Status; once in, the host’s scene loads by itself.
public abstract void Join(INetSocket socket, EndPoint host)Joins the host at host over a socket of the caller’s choosing (e.g. a MemoryNetwork’s).
public abstract void Leave(string reason = "Left the game.")Leaves the game (or stops hosting it, which ends it for everyone). This machine carries on by itself.
OwnerOf
Section titled “OwnerOf”public abstract NetPlayer? OwnerOf(Entity? entity)The player entity (or what it’s part of) belongs to: their character, or what they fired.
PlayerById
Section titled “PlayerById”public abstract NetPlayer? PlayerById(int id)The player with this Id, if they’re in the game.
public abstract void Send(string message, object? data = null)Sends message to every machine, this one included, where it arrives as if sent with Game.Send: Sender says who sent it. Data can be a number, text, a vector, a color, an entity or a player. Offline it’s just Game.Send.
SendCommand
Section titled “SendCommand”public abstract void SendCommand(string name, object? args)Sends a command for the host to carry out for this player (a strategy order, crafting). Offline, and on the host, it runs here.
SendCommandTo
Section titled “SendCommandTo”public abstract void SendCommandTo(NetPlayer player, string name, object? data)On the host, tells one player’s machine something (what the host’s run of their tools wants shown there): name is run there with data (see OnHostCommand), once they’re in this scene. Nothing elsewhere.
TeamNames
Section titled “TeamNames”public abstract IReadOnlyList<string> TeamNames()The teams’ names (the host’s Teams on a player’s machine; the scene’s offline).
Events
Section titled “Events”public event Action<string>? EndedRaised when this machine’s game ends: the host left, the connection was lost, or joining failed. With why.
PlayerJoined
Section titled “PlayerJoined”public event Action<NetPlayer>? PlayerJoinedRaised on every machine when someone joins (also sent as the message “PlayerJoined”, with the player).
PlayerLeft
Section titled “PlayerLeft”public event Action<NetPlayer>? PlayerLeftRaised on every machine when someone leaves (also sent as the message “PlayerLeft”, with the player).