# PokiSDK: Phaser 3

Integrating the Poki SDK in Phaser 3: the same five event moments as every engine, with Phaser 3-specific setup.

[Source](https://developers.poki.com/guide/sdk-phaser)

## Integration outline

1.  **Set up the SDK** for Phaser 3. Use the official Poki Phaser plugin; an example project is available in the plugin repository.
2.  **Signal loading:** fire `gameLoadingFinished()` when your game is done loading, so conversion to play is measured correctly.
3.  **Gameplay events:** fire `gameplayStart()` on the player's first input and every return to gameplay; `gameplayStop()` on every interruption (pause, level end, game over, menu).
4.  **Ads:** implement `commercialBreak()` at natural breaks before returning to gameplay, and `rewardedBreak()` for opt-in rewards. Mute audio and disable input during ads.
5.  **Test** with the [Poki Inspector](https://developers.poki.com/guide/inspector) to verify the event flow.

Event rules and the full order-of-events reference live in the [SDK overview](https://developers.poki.com/guide/sdk-overview).

This is the official Poki Phaser plugin for [Phaser 3](https://phaser.io/phaser3). It automates most of implementing the Poki SDK when building a game with Phaser. You can also check the example in the [/example](https://github.com/poki/phaser-plugin/tree/main/example) directory.

## Features

-   Injects and loads the Poki SDK for you (asynchronously)
-   Triggers the `gameLoadingFinished` event when loading
-   `gameplayStart` and `gameplayStop` events fire automatically when your game scene starts and stops
    -   `commercialBreak` is requested before `gameplayStart` is fired
-   Disables input and audio during video ads
-   Gives you easy, global access to the SDK using `scene.plugins.get('poki')`, so you can:
    -   request a `rewardedBreak`
    -   or manually call `gameplayStart` / `gameplayStop`

## Install the plugin

First, add the `@poki/phaser-3` plugin as a dependency to your project:

```bash
$ npm install --save-dev @poki/phaser-3
# or
$ yarn add --dev @poki/phaser-3
```

Now add the plugin to the Plugins section of your Phaser configuration, for example:

```javascript
import { PokiPlugin } from '@poki/phaser-3'
// ...
const config = {
  // ...
  plugins: {
    global: [
      {
        plugin: PokiPlugin,
        key: 'poki',
        start: true, // must be true, in order to load
        data: {
          // This must be the key/name of your loading scene
          loadingSceneKey: 'LoadingScene',
          // This must be the key/name of your game (gameplay) scene
          gameplaySceneKey: 'PlayScene',
          // This will always request a commercialBreak when gameplay starts,
          // set to false to disable this behaviour (recommended to have true,
          // see Poki SDK docs for more details).
          autoCommercialBreak: true
        }
      }
    ]
  }
}
// ...
var game = new Phaser.Game(config)
```

For more on Phaser's configuration, see the [Phaser 3 configuration notes](https://rexrainbow.github.io/phaser3-rex-notes/docs/site/game/).

## Game loading logic

The Poki Phaser plugin automatically calls `gameLoadingFinished()` if `loadingSceneKey` is configured. Make sure your loading scene is included in the config object.

```javascript
const config = {
  // ...
  plugins: {
    global: [
      {
        data: {
          loadingSceneKey: 'LoadingScene'
        }
      }
    ]
  }
}
```

## Gameplay events

The Poki Phaser plugin automatically calls `gameplayStart()` and `gameplayStop()` if `gameplaySceneKey` is configured. Make sure your gameplay scene is included in the config object.

```javascript
const config = {
  // ...
  plugins: {
    global: [
      {
        data: {
          gameplaySceneKey: 'PlayScene'
        }
      }
    ]
  }
}
```

## Final steps

### Additional helpful methods

#### Execute a function only on initialization

To run code only once the PokiSDK is initialized, use this interface:

```javascript
const poki = scene.plugins.get('poki') // get the plugin from the Phaser PluginManager

poki.runWhenInitialized((poki) => {
  // This is called after the PokiSDK is fully initialized, or immediately if
  // the PokiSDK has already been initialized.
})
```

#### Fire SDK events manually

If your game doesn't use multiple scenes for gameplay, you can call the events manually:

```javascript
const poki = scene.plugins.get('poki') // get the plugin from the Phaser PluginManager

poki.gameplayStart()
// ... start gameplay ...
scene.on('player_died', () => {
  poki.gameplayStop()
})
```

### Upload and test your game in Poki for Developers

Congratulations, you've implemented the PokiSDK. Now upload your game to the [Poki Inspector](https://inspector.poki.dev/) and test it there. When you're happy with the implementation, send us a review request and we'll play the game. If you get stuck, reach out via Discord or `developersupport@poki.com`.
