Session operations as a client

Manage your sessions with the operations available to you as a session client.

Note: The Multiplayer Services SDK uses sessions to manage groups of players. Sessions relies internally on different combinations of Unity Gaming Services such as Relay, Distributed Authority, Lobby, Matchmaker and Multiplay Hosting, and thus contributes to the billing of those services.

As a session client, there are certain operations available to you for interacting with a session. Note that some of these operations differ from those available to the host.

Session property operations

A client can read properties from a session. A client can't add, update, or delete session properties; only the host has those permissions.

Note: If you are the host and have an ISession (client session), you can call ISession.AsHost() to get an IHostSession (host session) from your ISession. After you have an IHostSession, you can add, update, or delete session property. Refer to Session operations as a host.

Read APIs

The following APIs allow a client to read session properties:

ISession.IReadOnlyDictionary<string, SessionProperty> Properties { get; }

Read a session property

The following code snippet demonstrates how to read the colour property:

if (clientSession.Properties.TryGetValue("colour", out var colour))
{
    Debug.Log($"The colour session property is {colour.Value}");
}

Player property operations

A client can access any player properties that have been set to be visible to them. However, a client can only add, update, or remove the player properties they own.

Read APIs

The following APIs allow a client to read player properties:

IPlayer ISession.CurrentPlayer { get; }
IReadOnlyList<IReadOnlyPlayer> ISession.Players { get; }

Note: Use ISession.CurrentPlayer to read your own properties.

Read a player property

The following code snippet demonstrates how to read the colour player property:

if (clientSession.Players.First().Properties.TryGetValue("colour", out var colour))
{
    Debug.Log($"First player colour property is {colour.Value}");
}

Write APIs

The following APIs allow a client to modify its player properties:

IPlayer ISession.CurrentPlayer { get; }
Task ISession.SaveCurrentPlayerDataAsync();
void IPlayer.SetProperty(string key, PlayerProperty property);
void IPlayer.SetProperties(Dictionary<string, PlayerProperty> properties);

Add a player property

To add a property, you must decide upon the visibility of the property:

  • Public (visible to everyone)
  • Member (visible to members of the Session)
  • Private (visible to the player who owns the property and the host)

The following code snippet demonstrates how to add a colour property with the value red on a client:

var redColourProperty = new PlayerProperty("red");
clientSession.CurrentPlayer.SetProperty("colour", redColourProperty);
await clienSession.SaveCurrentPlayerDataAsync();

Update a player property

The following code snippet demonstrates how to set the colour property to null on a client:

var nullColourProperty = new PlayerProperty(null);
clientSession.CurrentPlayer.SetProperty("colour", redColourProperty);
await clienSession.SaveCurrentPlayerDataAsync();

Delete a player property

The following code snippet demonstrates how to delete the colour property on a client:

clientSession.CurrentPlayer.SetProperty("colour", null);
await clienSession.SaveCurrentPlayerDataAsync();