# PokiSDK: Godot

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

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

## Integration outline

1.  **Set up the SDK** for Godot. Add the Poki SDK integration for Godot web exports.
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).

Made by [vkrishna](https://twitter.com/vkrishna1983).

This plugin works for [Godot](https://godotengine.org/) 3.4 and above. It helps you integrate the PokiSDK into your Godot (3.4.x) game. You can build the integration yourself by creating a custom HTML shell from the default template, but this plugin makes it faster and easier.

## What the plugin provides

-   An export preset for the Poki platform
-   A custom HTML shell
-   A PokiSDK singleton for GDScript integration
-   A demo scene showcasing usage

Once you install the plugin and reload the project, a new HTML5 export preset called `Poki` appears. It provides the core integration through a custom HTML shell, and you can make API calls through the autoloaded `PokiSDK` singleton. See the example in the [/example](https://github.com/vkbsb/godot-poki-sdk/blob/master/addons/demo/demo.gd) directory.

## GDScript API and signals

```gdscript
- PokiSDK.gameplayStart() #-- in JS : PokiSDK.gameplayStart()
- PokiSDK.gameplayStop() #-- in JS : PokiSDK.gameplayStop()
- PokiSDK.commercialBreak() #-- in JS : PokiSDK.commercialBreak()
- PokiSDK.rewardedBreak() #-- in JS : PokiSDK.rewardedBreak()
- PokiSDK.shareableURL(params) #-- in JS : PokiSDK.shareableURL({}).then(url => {})
#Signals available from the PokiSDK 

#Triggered as soon as the commercial break is over.
commercial_break_done()

#Triggered once the rewarded break has finished. Response indicates if the ad was successfully played or not.
rewarded_break_done(response) 

#Triggered once the shareableURL is ready for use.
shareable_url_ready(url)
```

## Install the plugin

There are several ways to install the plugin. To install from the official [Asset Library](https://godotengine.org/asset-library/asset/1388), follow the on-screen steps. Alternatively, download the archive directly from [godot-poki-sdk-master.zip](https://github.com/vkbsb/godot-poki-sdk/archive/refs/heads/master.zip). Or download the source and copy the `poki-sdk` directory into your project's `addons` directory.

```bash
git clone https://github.com/vkbsb/godot-poki-sdk.git
```

## Export your project

Enable the plugin from `Project > Plugins`, then reload the project via `Project > Reload Current Project`. Once installed, export your preset via `Project > Export...` and choose `Poki` from the presets. The extension:

-   Adds a new preset called `Poki` to the export config in your project.
-   Adds an automatically loaded singleton called `PokiSDK` for your game script to use.

## Gameplay events

Use `gameplayStart()` to mark when players are playing your game (for example level start and unpause). Use `gameplayStop()` to mark when they aren't (for example level finish, game over, pause, or quit to menu).

```gdscript
# first level loads, player clicks anywhere
PokiSDK.gameplayStart()
# player is playing
# player loses round
PokiSDK.gameplayStop()
# game over screen pops up
```

## commercialBreak

Commercial breaks show video ads and should be triggered at natural breaks in your game. Throughout the game we recommend calling `commercialBreak()` before every `gameplayStart()`, that is, whenever the player has shown intent to continue playing.

```gdscript
# register callback to respond to commercial break playback
PokiSDK.connect("commercial_break_done", self, "on_commercial_break_done")

# trigger pause gameplay event.
PokiSDK.gameplayStop()

# trigger the commercial break 
PokiSDK.commercialBreak()

# callback function to handle the commercial break response
func on_commercial_break_done(response):
	print("Commercial break done", response)
	PokiSDK.gameplayStart()
```

Important information about commercialBreaks Not every `commercialBreak()` will trigger an ad. Poki's system decides when a player is ready for another ad, so feel free to signal as many commercial break opportunities as possible.

## rewardedBreak

Rewarded breaks let a player choose to watch a rewarded video ad in exchange for a benefit in the game (more coins, for example). When using `rewardedBreak()`, make it clear to the player beforehand that they're about to watch an ad.

```gdscript
# register callback to respond to rewarded break playback
PokiSDK.connect("rewarded_break_done", self, "on_reward_break_done")

# trigger pause gameplay event
PokiSDK.gameplayStop()

# trigger the rewarded break 
PokiSDK.rewardedBreak()

# callback function to handle the reward break response
func on_reward_break_done(response):
    print("Rewarded break done", response)
    if response:
        print("Reward gained!")
    else:
        print("No Reward.")
    PokiSDK.gameplayStart()
```

About the rewardedBreak timer `rewardedBreak()` affects the timing of `commercialBreak()`: when a player interacts with a rewarded break, our system's ad timer is reset so they don't immediately see another ad.

## Final steps

### Disable sound and input during ads

Make sure audio and keyboard input are disabled during commercial breaks so the game doesn't interfere with the ad. You can pause the audio like this:

```gdscript
//$AudioStreamPlayer.stream_paused = true
```

### Shareable URLs and URL manipulation

You can create a shareable URL with the following:

```gdscript
# register callback to respond 
PokiSDK.connect("shareable_url_ready", self, "on_shareable_url_ready")

# call the function with some data to encode
PokiSDK.shareableURL({"a":1, "b":2})

# implement the callback function
func on_shareable_url_ready(url):
	print("URL: ", url)
	$Label.text = url
```

### 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`.
