Class NetPlayer

Namespace
Unity.Multiplayer.Core

One player in a session, as every peer sees them (UM-T55): the seat they sit in (stable across a reconnect), the connection they are on now, their name and colour, whether they host, their round-trip time to the server, and whether they are this peer's own player. The server fills it from the connection approval payload and keeps every peer's NetRoster in step.

public sealed class NetPlayer : IEquatable<NetPlayer>
Inheritance
NetPlayer
Implements
Inherited Members

Remarks

Immutable: the roster hands out a new record whenever something about the player changes. Compare records by Seat, which identifies a player for as long as their seat lives; ClientId changes when they reconnect.

Constructors

NetPlayer(int, ulong, string, PlayerColor, bool, bool, int, bool)

Builds a record.

public NetPlayer(int seat, ulong clientId, string displayName, PlayerColor color, bool isHost, bool isConnected, int pingMs, bool isLocal = false)

Parameters

seat int

The seat index, 0 or more.

clientId ulong

The connection's client id now (the last one while isConnected is false).

displayName string

The name everyone sees; null is stored as empty.

color PlayerColor

The colour everyone draws the player in (already resolved: see Resolve(PlayerColor, int)).

isHost bool

True for a listen server's own player.

isConnected bool

False while the player has dropped and their seat is held for a reconnect.

pingMs int

Round-trip time to the server in milliseconds; UnknownPing when not measured.

isLocal bool

True on the peer whose own player this is.

Exceptions

ArgumentOutOfRangeException

seat is negative.

Fields

UnknownPing

PingMs before the server has measured it (and always for a dedicated server's own view of nobody).

public const int UnknownPing = -1

Field Value

int

Properties

ClientId

The client id of the player's connection now (of the last one while IsConnected is false).

public ulong ClientId { get; }

Property Value

ulong

Color

The colour everyone draws the player in: their own, or their seat's palette colour when they never picked one.

public PlayerColor Color { get; }

Property Value

PlayerColor

DisplayName

The name everyone sees.

public string DisplayName { get; }

Property Value

string

IsConnected

False while the player has dropped and their seat is held for them to reconnect.

public bool IsConnected { get; }

Property Value

bool

IsHost

True for a listen server's own player (the host).

public bool IsHost { get; }

Property Value

bool

IsLocal

True on the peer whose own player this is.

public bool IsLocal { get; }

Property Value

bool

PingMs

Round-trip time between the player and the server in milliseconds (0 for the host); UnknownPing until measured.

public int PingMs { get; }

Property Value

int

Seat

The seat index: who this player is for as long as their seat lives, across reconnects.

public int Seat { get; }

Property Value

int

Methods

Equals(object)

Determines whether the specified object is equal to the current object.

public override bool Equals(object obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

Equals(NetPlayer)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(NetPlayer other)

Parameters

other NetPlayer

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

FromSeat(Seat, bool, int, ulong?)

The record a server keeps for seat: the name as Clean(string, string) makes it, the colour Resolve(PlayerColor, int)d for the seat, the host flag for a listen server's own seat (client 0), and connected while the seat is occupied.

public static NetPlayer FromSeat(Seat seat, bool listenServer, int pingMs, ulong? localClientId)

Parameters

seat Seat

The server's seat.

listenServer bool

True when the server is a Host (its own player sits in a seat).

pingMs int

The connection's measured round-trip time; ignored (unknown) while the seat is held.

localClientId ulong?

The server's own client id when it plays (a Host), else null.

Returns

NetPlayer

Exceptions

ArgumentNullException

seat is null.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.

WithIdentity(string, PlayerColor)

This record with a new name and colour.

public NetPlayer WithIdentity(string displayName, PlayerColor color)

Parameters

displayName string
color PlayerColor

Returns

NetPlayer

WithLocal(bool)

This record with IsLocal set to isLocal.

public NetPlayer WithLocal(bool isLocal)

Parameters

isLocal bool

Returns

NetPlayer