Manage properties as a host
Manage your sessions with the operations available to you as a session host.
Read time 2 minutesLast updated 3 months 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 the property:
colourif (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 property with the value in :
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 the property to in .
colournullhostSessionvar 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 property in .
colourhostSessionhostSession.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 player property.
colourif (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 property with the value in , which is the first client in the list of clients:
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 the property to in :
colournullfirstPlayervar 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 property in :
colourfirstPlayervar firstPlayer = hostSession.Players.First()firstPlayer.SetProperty("colour", null);await hostSession.SavePlayerDataAsync(firstPlayer.Id);
Elect a new session host
The following code snippet demonstrates how to elect a given by its ID as the new host:
playerpublic Task ElectPlayerAsHostAsync(string playerId){ _session.AsHost().Host = player.Id; return _session.AsHost().SavePropertiesAsync();}
After setting the 's host ID, save the changes to take effect.
session