Session operations as a host

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

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 host, there are certain operations available to interact with a session. Note that some of these operations differ from those available to all clients.

Session property operations

The host can read, add, update, or remove properties from a session.

Read APIs

The following APIs allow the host to read session properties:

IReadOnlyDictionary<string, SessionProperty> ISession.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}");
}

Write APIs

The following APIs allow the host to modify session properties:

void IHostSession.SetProperties(Dictionary<string, SessionProperty> properties);
void IHostSession.SetProperty(string key, SessionProperty property);
Task IHostSession.SavePropertiesAsync();

Add a session property

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

  • Public (visible to everyone and is included in query result)
  • Member (visible to members of the session)
  • Private (visible to the member who set it)

After you decide the required visibility, you must create the session property, set it, and save it to the session. The following code snippet demonstrates how to add a colour property with the value red in hostSession:

var redColourProperty = new SessionProperty("red", VisibilityOptions. Public);
hostSession.SetProperty("colour", redColourProperty);
await hostSession.SavePropertiesAsync();

Update a session property

The following code snippet demonstrates how to set the colour property to null in hostSession.

var nullSessionProperty = new SessionProperty(null, VisibilityOptions. Public);
hostSession.SetProperty("colour", nullSessionProperty);
await hostSession.SavePropertiesAsync();

Delete a session property

The following code snippet demonstrates how to delete the colour property in hostSession.

hostSession.SetProperty("colour", null);
await hostSession.SavePropertiesAsync();

Player property operations

The host can read, add, update, or remove properties from any player.

Read APIs

The following APIs allow the host to read player properties:

IReadOnlyList<IPlayer> IHostSession.Players { get; }
IReadOnlyDictionary<string, PlayerProperty> IReadOnlyPlayer.Properties { get; }

Read a player property

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

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

Write APIs

The following APIs allow the host to modify player properties:

IReadOnlyList<IPlayer> IHostSession.Players { get; }
Task IHostSession.SavePlayerDataAsync(string playerId);
void IPlayer.SetProperty(string key, PlayerProperty property);
void IPlayer.SetProperties(Dictionary<string, PlayerProperty> properties);

Add a player property

To add a property to a player, 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 in firstPlayer, which is the first client in the list of clients:

var firstPlayer = hostSession.Players.First()
var redColourProperty = new PlayerProperty("red");
firstPlayer.SetProperty("colour", redColourProperty);
await hostSession.SavePlayerDataAsync(firstPlayer.Id);

Update a player property

The following code snippet demonstrates how to set the colour property to null in firstPlayer:

var firstPlayer = hostSession.Players.First()
var nullColourProperty = new PlayerProperty(null);
firstPlayer.SetProperty("colour", nullColourProperty);
await hostSession.SavePlayerDataAsync(firstPlayer.Id);

Delete a player property

The following code snippet demonstrates how to delete the colour property in firstPlayer:

var firstPlayer = hostSession.Players.First()
firstPlayer.SetProperty("colour", null);
await hostSession.SavePlayerDataAsync(firstPlayer.Id);