ข้ามไปยังเนื้อหา
InstaPlay
หัวข้อเอกสาร SDK

Portal events

InstaPlay.on(event, callback)

A subscription returns an unsubscribe function:

const off = InstaPlay.on('pause', ({ reason }) => game.pause(reason))
InstaPlay.on('resume', () => game.resume())
off()
Event When Payload
pause the tab is hidden, a portal window is open (report, sign-in from the game), an ad, the app is in background { reason: 'hidden' / 'overlay' / 'ad' / 'app_background' }
resume the pause reason is gone —
mute / unmute the sound button of the portal player; ads (SDK 1.3) — while an ad is shown —
visibility the tab is shown or hidden { visible }
localeChanged the player changed the portal language { locale }
playerChanging SDK 1.1: the player signed in, signed out or changed in another tab; SDK 1.4 — also a sign-in from the game to another account. The portal restarts the game in about 1.5 s { reason: 'login' / 'logout' / 'switch' }

A game must stop its loop and sound on pause and silence sound on mute — a catalog requirement. The portal remembers the sound choice itself: the initial state (sound off before start, the game paused) is in InstaPlay.system.muted and InstaPlay.system.pause right after init (lifecycle).

Ads: pause { reason: 'ad' } and mute

During an ad (ads) the portal pauses and mutes the game itself:

  1. pause with { reason: 'ad' } and mute — before the ad;
  2. resume and unmute — after the ad.

If the player muted the game with the player button before the ad, no unmute comes after it — ads do not turn on sound the player turned off. If the pause has another reason too (a hidden tab, a portal window), resume comes when the last one is gone. Ads need no separate handling — regular pause and mute are enough.

Player change: playerChanging

Was → became reason
guest → account (sign-in, including a guest upgraded to an account in place) login
account → guest (sign-out) logout
account → another account, guest → another guest switch

The reasons are the same on the portal and in the instaplay dev emulator (there “Player A → Player B” is switch). On logout the previous player signed out — send their data while their token is still valid; on switch it is another person or another profile of the same kind.

InstaPlay.on('playerChanging', async ({ reason }) => {
  // reason: 'login' | 'logout' | 'switch' (see the table below)
  const token = await InstaPlay.player.getToken() // the previous player's valid token, from the cache
  navigator.sendBeacon('https://api.my-game.example/flush', JSON.stringify({ token, state }))
})

The game has about 1.5 seconds before the restart. In this window the portal no longer accepts the previous player's calls: saves and leaderboards answer session_expired. Send your data to your own server — getToken() returns a still valid token from the SDK cache. After the restart the game starts with the new player; a guest upgraded to an account in place via the portal header or another tab is a login too.

Sign-in from the game: player.login() and playerChanging

SDK 1.4 (player): while the sign-in window is open the game gets pause { reason: 'overlay' }, then resume. What follows depends on the outcome:

login() outcome Events
signed_in, same player.id (the guest became an account in place) no playerChanging, no restart — the game goes on
signed_in, different player.id (sign-in to an existing account) first the login() reply, then playerChanging { reason: 'login' } and a restart under the account in about 1.5 s
already_signed_in, cancelled nothing

After a sign-in to another account the SDK token cache is already dropped: getToken() in the window before the restart will not return the previous guest's token. Send data only to your own server with the session it already issued, not to saves. Your server gets the previous guest pseudonym in the merged claim of the account token (a guest became an account).