Cloud saves in practice
This guide shows how to design your game's save data for the data module, a localStorage-shaped store with synchronous reads, background writes and 1 MiB for the whole save, and how it behaves for guests who sign in mid-game. Examples assume const SDK = window.ZokoGames.SDK; and await SDK.init(); as in the SDK introduction; unfamiliar terms are in the glossary.
Turn the module on first: in the console, tick Uses the SDK data module on the game's listing. While it is off, every SDK.data call throws dataModuleDisabled. The four methods (getItem, setItem, removeItem, clear) are synchronous and take and return strings, and they throw the error object rather than rejecting.
One key or many#
Each setItem is cheap, and several writes in a burst become a single upload. Prefer a few focused keys over one giant blob, so a corrupt value does not lose everything:
SDK.data.setItem("profile", JSON.stringify({ v: 2, name, coins, unlocked }));
SDK.data.setItem("settings", JSON.stringify({ v: 1, music, sfx }));
SDK.data.setItem("run", JSON.stringify({ v: 1, level, seed })); // small checkpoint, not a replayThe 1 MiB limit counts the whole save serialised as JSON. Store ids and numbers, not world snapshots. A setItem that would break the limit throws dataLimitExceeded and writes nothing.
Version every value#
Players keep saves for years. Put a version in each value and migrate on load:
function load(key, version, migrate, fallback) {
try {
const raw = SDK.data.getItem(key);
if (!raw) return fallback;
let obj = JSON.parse(raw);
while (obj.v < version) obj = migrate[obj.v](obj);
return obj;
} catch {
return fallback; // corrupt or unreadable: start clean, never crash
}
}Write at meaningful moments#
Save when something worth keeping changed (level complete, purchase, setting), not every frame. The SDK debounces for one second, uploads at least every 30 seconds while writes continue and flushes when the page is hidden or closed. Do not build your own timer around that.
Guests and signing in#
Signed-in players have a save on the portal's servers. Guests have a save in the browser. When a guest signs in during play:
- If the account has no save for your game, the guest's progress is uploaded and becomes the account's save.
- If the account already has a different save, the portal asks the player which one to keep: Keep this device's progress or Use my saved progress. It asks the same when a signed-in player opens your game while the device still holds different guest progress; the game then starts from the device copy. Until the player answers, your game runs on the guest's progress and its
SDK.datawrites stay on the device, never uploaded. Keep this device's progress uploads the newest device copy, those writes included, and your game carries on without a reload. Use my saved progress drops those writes and reloads your game:init()runs again andSDK.dataholds the account's save. Writes your game makes while the portal is still sending the guest's progress at sign-in count as the guest's too. A choice made in one tab closes the question in the player's other open tabs, which follow it. If the player uses Delete cloud save while the question is open, only the account's copy is deleted: the device copy stays and becomes the account's save when your game next loads. - If the player signs in while offline, or the portal cannot read the account's save, nothing is asked and nothing is overwritten: the guest's progress stays on the device, and the comparison runs when the connection returns or on the next load.
The portal does the upload and asks the question for you, so you do not write anything extra when the player signs in. It happens in the background, right after your auth listener is called, so do not assume it has finished. No SDK event reports the question or the answer, and SDK.data never changes under a running game: your game keeps the data it started with until it is reloaded. If your game had saved nothing yet and the account has a save, the portal offers the player a reload instead of asking. Signing out restarts the game as a guest. Keep saves small and versioned so either outcome loads cleanly on the next start.
After a sign-out, or a switch straight to another account (which also restarts the game), SDK.data calls keep working as usual until the restart, but nothing your game writes in that window is stored. When a signed-in player signs out with changes not uploaded yet (offline, or a failed upload) and later signs in again with the same account, those changes are uploaded first, before any guest progress on the device is compared with the account, instead of being replaced by the older cloud save; if another device saved in the meantime, the portal asks which copy to keep.
SDK.user.addAuthListener(() => {
// The merge is still running. Refresh your account UI here;
// do not rewrite keys from stale in-memory state, and do not start a new save slot.
hud.refreshAccount();
});Two devices#
A signed-in player can play on two devices. If another device saved since this one last synced, the portal pauses uploads and asks the player which copy to keep; choosing the other device's copy reloads your game so it starts from it. Nothing in your code is needed, which is one more reason to load all state at start and never cache it across sessions yourself.
Keep the browser-local fallback honest#
Do not use localStorage for progress. Inside the portal's frame it may be partitioned or cleared; SDK.data follows the player. If you are migrating existing players, copy old keys once (see data).
Ported games that use localStorage#
If a game saves with localStorage and you cannot change its code, SDK 1.4.0 can mirror those writes into the cloud save: see mirroring localStorage.
Ported games that use IndexedDB#
Unity (/idbfs), Godot and GameMaker save into IndexedDB. SDK 1.7.0 can mirror those databases too: await SDK.data.mirrorIndexedDB({ databases: ["/idbfs/mygame"] }) after init() and before the engine starts, so the restore finishes before the game reads its files. See mirroring IndexedDB for the limits: the snapshot shares the 1 MiB save, a save that does not fit is skipped, never cut, and a save in the last moments before the tab closes can be lost. Keep big asset caches out of databases.
When the SDK is disabled#
Without the SDK (SDK.environment is "disabled": not on localhost and not inside the portal frame, or before init() finishes), SDK.data throws sdkDisabled or sdkNotInitialized. Wrap storage in a tiny adapter that falls back to localStorage for standalone runs:
await SDK.init();
const live = SDK.environment !== "disabled";
const store = {
get: (k) => (live ? SDK.data.getItem(k) : localStorage.getItem(k)),
set: (k, v) => (live ? SDK.data.setItem(k, v) : localStorage.setItem(k, v)),
};Things not to store#
- Anything that must be authoritative or tied to payments: keep it on your server and verify the player with the token.
- Secrets, tokens and personal data.
- Large assets and replays.
Testing#
- Local mode (your own
localhostpage) keeps the whole save as one JSON object inlocalStorageunderzoko-sdk-data; delete that key for a new player. Nothing is uploaded and there is no merge or two-device prompt, so test those on the live portal with two accounts. - Offline or ad-blocked players: the module needs no ads, and while the player is offline it reads and writes the device copy and uploads when the connection returns (offline play).
- Pass
?user_response=logged_outto test the guest path, then?show_auth_prompt_response=user1and press your sign-in button to test the upgrade. - In the Preview tool the "Data module used for saves" checklist item ticks when your game writes.