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

Game lifecycle in the SDK

InstaPlay.init(options?)

The first call. Resolves with InitResult:

Field Value
environment instaplay (portal), local (localhost or ?instaplay_local=1), disabled (outside the portal)
locale ru or en — the portal page language
applicationType web, pwa or app
device { type: 'desktop' / 'mobile' / 'tablet', touch, os }
player a guest or an account: { id, kind, nickname, avatar } (see Player)
features what is available: lifecycle, player, saves, leaderboard, analytics; SDK 1.1 — also token (a player token for your server), 1.3 — ads (ads), 1.4 — login (sign-in from the game, player)
session SDK 1.1: ok — the player is real; none — no portal session, player is the p_guest stub; local — local and disabled modes. Without ok do not use player.id as a key on your server
country SDK 1.2: the player country — an ISO 3166-1 alpha-2 code ('DE') or null. Currently the portal does not pass it (null); outside the portal it is always null. Only for picking a region or server, not as a player key (player region)

init({ debug: true }) logs the portal exchange to the console. The init timeout is 5 s: if the portal does not answer, you get environment: 'disabled' and the game keeps running.

After init:

  • InstaPlay.environment — the current environment (disabled before init);
  • InstaPlay.version — the SDK version;
  • InstaPlay.isSupported(feature) — whether the platform has this feature;
  • InstaPlay.getServerTime() — server time in ms (by the offset received in init);
  • InstaPlay.system — { locale, device, applicationType, muted, pause }: SDK 1.1 knows the portal sound and pause from init (muted: true — the player turned sound off before pressing Play; pause — null or { reason }), then the values follow the mute/unmute and pause/resume events (events);
  • InstaPlay.isError(e) — an SDK error with a code field (see Errors and limits).

Environment before the SDK loads

The portal opens the game with the frame URL parameters instaplay_origin (the portal origin) and instaplay_locale (ru or en, the page language). This is a stable contract: with them a game can decide “I am on the portal” and pick a language before the SDK loads — for example, skip its own sign-in screen and its own providers. The SDK checks instaplay_origin against the real frame parent: a forged parameter gives environment: 'disabled', so rely on init for SDK calls.

const params = new URLSearchParams(location.search)
const onPortal = params.has('instaplay_origin')
const lang = params.get('instaplay_locale') ?? navigator.language.slice(0, 2)

The portal speaks ru and en. If the game has more languages, pick yours by navigator.languages, and use instaplay_locale and localeChanged when the player's language is one of the portal's two.

Loading

  • InstaPlay.loading.progress(p) — loaded share 0…1; the portal draws a progress bar. The SDK sends at most 4 times per second and only when the value changes by 1 % or more.
  • InstaPlay.ready() — the game is ready (first frame drawn): the portal hides the loading screen and records the load time. No ready in 20 s — the portal offers a restart.

Gameplay

  • InstaPlay.gameplay.start() / InstaPlay.gameplay.stop() — the player is playing / menu, pause, end of level. The portal counts play time with them and shows no interstitial during play (ads: gameplay_active).
  • InstaPlay.game.happyMoment() — a bright moment (a record, a boss defeated).
  • InstaPlay.level.started(level, props?), InstaPlay.level.completed(level, props?), InstaPlay.level.failed(level, props?) — level events for analytics.
InstaPlay.level.started(3)
InstaPlay.gameplay.start()
// …
InstaPlay.gameplay.stop()
InstaPlay.level.completed(3, { stars: 2, timeMs: 41250 })