Skip to content

NetSession

namespace Griff.Runtime;
public sealed class NetSession

A network game in progress on this machine: hosting one, or joining or playing in someone else’s. A game has one only while it’s networked, as Session (Game.Network.Session): the game’s network system makes it when the game hosts or joins and lets it go when that ends. Who’s playing, scores and teams, and messages between machines are here; hosting, joining and the questions that also have an answer offline are on INetwork.

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. The session reaches the game only through its network system, which asks the systems that own each part.

public bool IsClient { get; }

True when playing in a game someone else hosts.

public bool IsConnecting { get; }

True from joining until the host lets this machine in (or doesn’t).

public bool IsHost { get; }

True on the machine running the game: a host, or a dedicated server.

public bool IsOnline { get; }

True while hosting or playing in someone else’s game (false while still joining).

public NetPlayer? LocalPlayer { get; }

The player on this machine (null on a dedicated server, and until joined).

public IReadOnlyList<NetPlayer> Players { get; }

Everyone in the game, the host first.

public NetRole Role { get; }

What this machine is in the game: Offline only while it’s still joining.

public NetPlayer? Sender { get; }

Who sent the message being handled right now (inside a listener for a message from another machine).

public string Status { get; }

A line about how it’s going, for a menu: “Hosting on 192.168.1.5:7777”, “Joining…”, why it ended.

public ValueTuple<long, long> Traffic { get; }

Bytes sent and received over the connection(s) so far.

public void AddScore(NetPlayer player, int points)

Adds points to a player’s score (on the host; everyone sees it).

public string Describe()

A line about the session for logs: who’s here, and on a player’s machine how the host’s updates are arriving.

public void Dispose()

Leaves any game in progress and closes the connection, as stopping the game does.

public bool IsMine(Entity? entity)

True if entity belongs to the player on this machine. Offline, true for everything.

public static bool IsUsedIn(Scene scene)

True if scene is set up for network play: it has a Multiplayer or something Networked, a button that hosts or joins a game, or a field for the address to join. The web player says network play isn’t there (Unavailable) when a scene like this starts.

public void Kick(NetPlayer player, string reason = "Removed by the host.")

Removes a player from the game (on the host).

public 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.

public static string LocalAddress()

This machine’s address on the local network, for others to join.

public NetPlayer? OwnerOf(Entity? entity)

The player entity (or what it’s part of) belongs to: their character, or what they fired.

public NetPlayer? PlayerById(int id)

The player with this Id, if they’re in the game.

public Entity? Respawn(NetPlayer player)

Gives a player a new character (on the host), e.g. after theirs was destroyed: at a PlayerSpawn, like when they joined. Replaces any they have.

public 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.

public void SendCommand(string name, object? args)

Sends a command for the host to carry out for this player (a strategy order, crafting). Offline it runs here.

public void SendCommandTo(NetPlayer player, string name, object? data)

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.

public void SendTo(NetPlayer player, string message, object? data = null)

Sends message to one player’s machine.

public void SendToHost(string message, object? data = null)

Sends message only to the host (the host’s own scripts hear it, with Sender set).

public void SetTeam(NetPlayer player, string team)

Moves a player to another team (on the host): their character changes color, and spawns at that team’s spawns.

public List<string> TeamNames()

The team names the host’s Multiplayer settings give, in order.

public const string AddressSaveKey

Where the address last joined is kept in the game’s save data.

public const string BrowserUnavailable

What Unavailable says in a browser.

public const int DefaultPort

The port games are hosted on unless a Multiplayer says otherwise.

public const string NameSaveKey

Where a player’s name is kept in the game’s save data.