Last month we released version 3.1 of the Unity Cloud Save SDK which added support for running Queries directly from a game client and for reading and writing to data in different Access Classes.
We just expanded the Unity SDK tutorial for Cloud Save to add examples of how to use Queries and Access Classes with both Player Data and Game Data (using Custom Items).
Queries and Indexes
Cloud Save Queries allows you to define indexes on Player Data and Game Data stored in Cloud Save so you can search / query data using Cloud Code. You must create an index before you save data that can be queried.
You can create and manage indexes in the Unity Cloud Dashboard.
Player Data: Access Classes
There are 3 different Access Classes for Cloud Save Player Data:
- Default: Readable and writable by the player the data corresponds to
- Public: Readable by any player, writable by the player the data corresponds to
- Protected: Readable by the player the data corresponds to, only writeable from a server
Game Data: Access Classes
Cloud Save supports 2 different Access Class levels for Custom Items - these can be for Game Data that does not have to be bound to a player:
- Default: Readable by any player, writeable only from a server
- Private: Readable and writeable only from a server
Example
If you want to create a system to allow players to find other players - for example for async matchmaking - you could first create an index for the names of the properties you want to be able to match on (e.g. language, location, etc).
Player 1:
In your game, you could then write data needed for matching to the Public Access Class for the Player, where it can be read (and queried) by other players. In this case, language and a location.
using SaveOptions = Unity.Services.CloudSave.Models.Data.Player.SaveOptions;
var data = new Dictionary<string, object> { { "language", "FR" }, { "location", "Paris" } };
await CloudSaveService.Instance.Data.Player.SaveAsync(data, new SaveOptions(new PublicWriteAccessClassOptions()));
Player 2:
You can then query from another client to get back a list of Player IDs for players that match a query:
var query = new Query(
new List<FieldFilter> {
new FieldFilter("language", "FR", FieldFilter.OpOptions.EQ, true),
new FieldFilter("location", "Paris", FieldFilter.OpOptions.EQ, true)
},
{ "location", "name", "avatar" } // List of Public data keys to return along with results
);
var results = await CloudSaveService.Instance.Data.Player.QueryAsync(query);
Debug.Log($"Number of players returned {results.Count}");
results.ForEach(r => {
Debug.Log($"Player ID: {r.Id}");
r.Data.ForEach(d => Log($"Key: {d.Key}, Value: {d.Value.GetAsString()}"));
});
You can also use Custom Items to Query Game Data in the same way, or use Unity Cloud Code (or a game server) to query across data in any Access Class, and for any Player or Custom Item.
You can use client side methods to quickly and easily to unlock new functionality without adding any server side logic and you can combine the functionality in Cloud Save with Cloud Code to create full featured systems for guilds, quests, community goals, NPCs, auction houses, inventory management, item trading, mailboxes, loot tables, etc