Game currency and cloud saves
VortexCredits.gameState stores one snapshot per signed-in account and game slug. A new account/game pair returns { game: '<slug>', balance: 0, data: {}, revision: 0 }. Load before writing. The first successful save returns revision 1, and each further successful write increments it. Keep the revision from the most recent load or successful write. data is the entire JSON object, so include fields you want to keep.
For queued changes, get the opaque game-account ID from VortexCredits.identity(). Pass it to gameState.load(account) and send it as account in gameState.save(). Arcade rejects the operation if the signed-in account has changed.
| Field | Contract |
|---|---|
balance |
Integer from 0 through 1,000,000,000 for gameplay currency. |
data |
JSON object up to 32 KiB (32,768 UTF-8 bytes after serialization). Arrays can be fields within the object. |
revision |
Server-managed write version. Send the revision of the snapshot on which your changes are based. |
Save meaningful checkpoints such as a finished run, a level unlock, or an explicit Save action. Debounce autosaves and serialize writes in each tab. Show Saving, Saved, and Changes pending according to the result. Keep pending changes until a confirmed save. Prefer saving before navigation, since backgrounding or closing a tab can interrupt the last write.
Handle concurrent saves
The example keeps the best score from either device and preserves the latest balance and unrelated fields. Call it from a serialized save handler after a completed run. The caller retains the score if the save fails and can offer a retry.
async function saveBestRun(score) {
if (!Number.isSafeInteger(score) || score < 0) {
throw new RangeError('Expected a non-negative integer score.');
}
const gameState = window.VortexCredits.gameState;
let base = await gameState.load();
for (let attempt = 0; attempt < 2; attempt += 1) {
const bestRun = base.data.bestRun ?? 0;
if (score <= bestRun) return base;
const data = { ...base.data, bestRun: score };
if (new TextEncoder().encode(JSON.stringify(data)).length > 32_768) {
throw new Error('Game save exceeds 32 KiB.');
}
try {
return await gameState.save({ balance: base.balance, data, revision: base.revision });
} catch (error) {
if (error.status !== 409 || !error.state || attempt === 1) throw error;
base = error.state;
}
}
}
Use a merge rule that fits each field. A personal best can take the maximum and achievement flags can merge by ID. For balances, consumable inventory, and other counters, record stable event IDs if you intend to replay pending changes. Apply each event once against the latest state. After a network failure, reload and check whether the intended event already landed before adding it again. If the game cannot merge safely, show both versions or ask the player which progress should be kept. Never overwrite a newer cloud save with an old local snapshot on reconnect.
Plan for updates, offline play, and service errors
- Put a game-owned
schemaVersioninsidedata. Migrate older saves when loading and preserve unknown fields where possible. Handle a newer format by keeping its saved data intact and asking the player to reload the latest game. The server'srevisionhandles concurrent writes. - Keep cloud writes compact and structured. Use JSON primitives, arrays, and plain objects. Leave headroom for future fields and avoid raw replays, media, secrets, payment data, and unnecessary personal information.
- Store guest progress under a separate local key if the browser allows it. Make guest-to-account import explicit and check the cloud save first. Scope any queued signed-in changes to the opaque account ID and the game, and verify the account before syncing. Allow play even when browser storage is blocked where the game supports that mode.
- On
401, keep pending changes and ask the player to sign in through Arcade. On409, useerror.stateto reconcile. On400or413, correct the payload or shrink it. On403,404, or410, stop writes and surface the service message. On503or a network failure, retain pending progress, then reload and reconcile when the service returns. - Use cloud saves for player-reported progress. Keep ranked scores, tradeable inventory, economy rules, and other authoritative outcomes on a game backend reviewed by Arcade. Keep wallet entitlements in the Arcade ledger even when local or cloud progress is reset.