Session operations as a host
Manage your sessions with the operations available to you as a session host.
Read time 2 minutesLast updated 12 hours ago
As a session host, you have access to operations that let you interact with session and player properties. Note that some host operations differ from those available to 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 thecolourif (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)
colourredhostSessionvar 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 thecolournullhostSessionvar 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 thecolourhostSessionhostSession.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 thecolourif (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)
colourredfirstPlayervar 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 thecolournullfirstPlayervar 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 thecolourfirstPlayervar firstPlayer = hostSession.Players.First()firstPlayer.SetProperty("colour", null);await hostSession.SavePlayerDataAsync(firstPlayer.Id);