Game SDK v1 quick start
公开文档 · 更新于
Scope
The script is served at /games/sdk/v1.js. Registered games communicate with the site player; standalone pages fall back to local storage. External submissions open as links and do not obtain account access by loading this script. Download the script for local testing if needed. The example ID must be replaced with an ID registered by the maintainer before deployment.
<script src="/games/sdk/v1.js"></script>
<script>
(async () => {
const gg = await GeekGravity.init({ game: 'my-game', version: '1.0.0' });
let state = { schemaVersion: 1, level: 1 };
function restore(data) {
// Validate, migrate older schemas and render the restored running state.
if (data && data.schemaVersion === 1) state = data;
}
gg.on('save', ({ data }) => restore(data));
const saved = await gg.save.load();
restore(saved.data);
gg.ready();
async function checkpoint() {
const result = await gg.save.write(state, { summary: { level: state.level } });
if (!result.saved) console.warn('Save failed. Keep the page open.');
else if (result.source !== 'cloud') console.info('Local save; cloud sync not confirmed.');
}
// Call checkpoint at stable progress milestones, not every frame.
})();
</script>
API
| API | Purpose |
|---|---|
gg.host |
web, app or standalone |
gg.ready() |
Tell the host that the game is ready |
gg.user() |
{ signedIn, name }, without authentication tokens |
await gg.requestLogin() |
Ask the host to sign in; check the returned signedIn value |
gg.on('pause', fn) / gg.on('resume', fn) |
Connect your pause and resume logic |
gg.on('user', fn) |
User-state changes |
gg.on('save', ({ data, rev }) => ...) |
Replace running state after another save is selected |
await gg.save.load() |
{ data, rev, source }; data may be empty |
await gg.save.write(data, { summary }) |
Save locally first, then attempt host sync; inspect saved and source |
gg.off(event, fn) |
Remove a listener; on also returns an unsubscribe function |
Use JSON-serializable data. Account save data is limited to 64 KiB and summary to 1 KiB. Keep large assets out of saves.
Conflicts
By default, the host asks the player which save to keep. Optionally supply a synchronous merge(local, cloud) function to init that returns serializable data. Only merge data with safe semantics, such as unlocked-level sets. Do not simply add currencies or choose maximum values for consumables or active runs.
Read save behavior. App support requires a compatible host implementation; detecting an App transport does not establish that every client version supports every feature. Verify the target client before release.