Skip to main content

Cloud Save

Cloud Save is a tool that allows developers to save and load data so their projects can have persistence. Some examples use cases is saving high scores, save game progress, and allows interaction with other players.

tip

Read Unity's Cloud Save documentation for more information.

Data Limit

caution

All data is currently limited to 400kb per Experience.

Requirements

These are the requirements for Cloud Save to work in an Experience:

  • The Experience manifest file needs to contain a valid set of tokens.
  • The Genies Sdk needs to be initialized before using any Cloud Save API.

Cloud Save API

String Keys

Keys are strings used to distinguish storage references and the data within a storage. They are unique among Experiences so a developer could use the same storage key for two different Experiences and not have the data be overwritten by the other Experience.

Cloud Save Storage

The CloudSaveStorage API provides multiple key value pairs of various types. Under the hood, it contains a Dictionary<string, object> and a bunch of helper methods to access provided data in a given format.

note

Cloud Save Storage requires the creation of a CloudSaveStorage reference. Then developers can use its methods to read and write data.

import { CloudSaveStorage } from "Genies.Components.Devkit.Experiences.CloudSave";

//Initialize the Storage Reference
let storage: CloudSaveStorage = new CloudSaveStorage(storageKey, isShared);

//Load from Storage and Read Storage Data
await storage.Load();
let boolValue: bool = storage.GetBool(boolKey);
let floatValue: float = storage.GetFloat(floatKey);
let stringValue: string = storage.GetString(stringKey);
let intValue: int = storage.GetInt(intKey);

//Save to Storage and Write Storage Data
storage.SetBool(boolKey, boolValue);
storage.SetFloat(floatKey, floatValue);
storage.SetString(stringKey, stringValue);
storage.SetInt(intKey, intValue)
await storage.Save();

//Check if Storage has a Data Key
console.log(storage.Has(dataKey))

Private and Shared Data

The CloudSaveStorage API uses an optional boolean parameter to have data be private or shared among users. By default, the data will be private.

Here is an example of private and shared data:

//Private Storage
let privateStorage = new CloudSaveStorage(privateKey);
let otherPrivateStorage = new CloudSaveStorage(otherPrivateKey, false);

//Shared Storage
let sharedStorage = new CloudSaveStorage(sharedKey, true);

TypeScript Example

Here's an example script that creates a private high score and a shared global score using the CloudSaveStorage API:

import { MonoBehaviour } from "UnityEngine";
import { GeniesSdk } from "Genies.Components.SDK.Core";
import { CloudSaveStorage } from "Genies.Components.Devkit.Experiences.CloudSave";

export default class MyScript extends MonoBehaviour {

async Start() {
await GeniesSdk.Initialize();

let highscoreStorage = new CloudSaveStorage("HighscoreStorageKey");
let globalscoreStorage = new CloudSaveStorage("GlobalscoreStorageKey", true);

await this.EstablishScoreData(highscoreStorage, "highscore");
await this.EstablishScoreData(globalscoreStorage, "globalscore");
}

private async EstablishScoreData(storage: CloudSaveStorage, scoreKey: string) {
//Load data from storage
await storage.Load();
//Check if key exists in data
if (storage.Has(scoreKey)) {
//Get key value from data
let score: int = storage.GetInt(scoreKey);
console.log("Storage of " + scoreKey + " has a value of " + score.toString());
}else{
console.log("Storage of " + scoreKey + " does not have a value yet");
//Set initial key value
storage.SetInt(scoreKey, 0);
//Save data to storage
await storage.Save();
console.log("Storage of " + scoreKey + " now has a value of 0");
}
}
}