Chuyển đến nội dung
InstaPlay
Các mục tài liệu SDK

InstaPlay SDK quick start

1. Add the script

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <script src="/_ip/sdk/v1.js"></script>
  </head>
  <body>
    <canvas id="game"></canvas>
    <script src="./game.js"></script>
  </body>
</html>

The path is absolute from the root of the game origin — it is correct for /<hash>/index.html too. Build check S02 does not treat /_ip/… as an error. With npm: npm i @instaplay/sdk and import InstaPlay from '@instaplay/sdk'.

2. Init and loading

const env = await InstaPlay.init()
console.log(env.environment, env.locale, env.player.nickname)

for (const [i, asset] of assets.entries()) {
  await load(asset)
  InstaPlay.loading.progress((i + 1) / assets.length)
}
InstaPlay.ready() // the portal hides its loading screen

3. A save and a score

const DEFAULTS = { level: 1, coins: 0 }
let state = DEFAULTS
try {
  const save = await InstaPlay.saves.get('progress')
  if (save) state = save.data // null — there is no save
} catch (e) {
  // unavailable / offline: the cloud did not answer and there is no copy on the device —
  // "unknown", not "none". Start from scratch, but do not assume there is no progress.
}

InstaPlay.gameplay.start()
// …gameplay…
// A write lands in the on-device buffer first; a rejection (no network or session) loses nothing.
InstaPlay.saves.set('progress', { level: 2, coins: 120 }).catch(() => {})
await InstaPlay.leaderboard.submit('main', score)
InstaPlay.gameplay.stop()

saves.get without a server answer and without a copy on the device rejects (unavailable or offline): without try/catch the game crashes on start. See “Reading” on the Saves page. The main board is declared in instaplay.json (see Leaderboards).

4. Pause and sound

InstaPlay.on('pause', () => game.pause())
InstaPlay.on('resume', () => game.resume())
InstaPlay.on('mute', () => audio.mute())
InstaPlay.on('unmute', () => audio.unmute())

5. Try it locally

npx instaplay dev ./build

The portal emulator embeds the game from another origin and shows the call log — see Local mode and emulator.