# Bring a Defold game

For a Defold game that already ships as an HTML5 bundle on other web game portals. You add the `zokogames` library, swap the other portal's extension and calls for `zoko.*`, and check the bundle settings. The full reference is the [Defold engine page](https://docs.zokogames.com/engines/defold). The overall route is in [bring your game](https://docs.zokogames.com/porting/overview).

## 1. Bundle settings

Add the library as in [Defold: install](https://docs.zokogames.com/engines/defold#install) and remove the other portal's extension from `game.project` dependencies. Then:

| Setting | On ZokoGames |
| --- | --- |
| SDK script tag | The module injects the SDK on the first `init`, but the upload validator reads your files and can report `no_sdk`. Use a custom HTML5 template (`htmlfile` in the `[html5]` section) with `<script src="https://sdk.zokogames.com/zokogames-sdk-v1.js"></script>` in its `<head>`, and remove the other portal's tag. |
| Architecture | `wasm-web`; add `js-web` only for very old browsers. No threaded variant: the portal frame is not cross-origin isolated. |
| Variant | Release, not Debug. |
| File names | As bundled. Do not rename files to `.br` or `.gz`. |
| Scale mode | One that keeps your aspect ratio, so the game fits a frame that changes with fullscreen and rotation. |

Details: [HTML5 export settings that matter](https://docs.zokogames.com/engines/defold#html5-export-settings-that-matter).

## 2. Replace the platform calls

`local zoko = require "zokogames.zokogames"`, call `zoko.init(callback)` once, then map each call. Calls that wait for the browser take `callback(err, result)` last; `err` is `nil` on success ([how calls work](https://docs.zokogames.com/engines/defold#how-calls-work)).

| Your game does | With `zoko` |
| --- | --- |
| Start the SDK | `zoko.init(callback)`; `zoko.is_available()` is false when the SDK is `disabled`, and the game must still run. |
| Loading finished | `zoko.game.loading_stop()` once the first collection can be played (optionally `loading_start()` and `loading_progress(p)` before it). The module does not send these on its own. |
| Gameplay start and stop | `zoko.game.gameplay_start()` and `gameplay_stop()`. |
| Midgame (interstitial) or rewarded ad | `zoko.ad.request_ad("midgame", { started = f, finished = f, error = f })` (or `"rewarded"`). Pause in `started`; resume in `finished` and in `error`. A request that cannot start calls only `error`. Grant a reward only in `finished`. |
| Mute | `zoko.game.add_settings_change_listener(fn)`. |
| Cloud save | `zoko.data.set_item(key, value)`, `zoko.data.get_item(key)` (`nil` when missing). `sys.save` files stay on one device; move progress you want kept here. |
| Leaderboard score | `zoko.user.submit_score(encrypted_score, score, callback)` for a signed-in player, on a board created in the console. No wrapper builds the encrypted payload; see [leaderboards](https://docs.zokogames.com/sdk/leaderboards#client-submission). |
| Purchases | `zoko.store.get_catalog_items`, `buy_item(sku, cb)`, `consume_item(sku, qty, cb)`. Purchases are test purchases for now ([purchases](https://docs.zokogames.com/sdk/purchases)). |
| Sign-in prompt, player | `zoko.user.show_auth_prompt(callback)`, `zoko.user.get_user(callback)`. |

A complete script is in [a working script](https://docs.zokogames.com/engines/defold#a-working-script).

The [compatibility shim](https://docs.zokogames.com/guides/migrating#two-ways-to-port) is for JavaScript call sites only; Lua calls move to `zoko.*`.

## 3. Build size and mobile

- Budget: 1,500 files, 250 MB unpacked, about 50 MB initial download (20 MB for mobile), estimated from uncompressed sizes.
- Texture profiles (Basis or ASTC for web), unused engine components removed in `game.project`.
- Load later levels with collection proxies (excluded resources) or live update.
- Keep the HTML5 heap modest for phones and start music from a "tap to play" screen. See [mobile](https://docs.zokogames.com/guides/mobile).

## 4. Test before you submit

1. **In the editor and desktop bundles** the Lua simulator answers (`zoko.is_simulated`). Edit `zoko.sim` before `init` to force ad errors, a logged-out player or a disabled SDK.
2. **The HTML5 bundle on `localhost`** runs the SDK's local mode, which also enforces the three-minute midgame cooldown the simulator skips. See [Defold: test locally](https://docs.zokogames.com/engines/defold#test-locally).
3. **Upload the zip** (bundle folder contents, `index.html` at the archive root) and open the game's **Preview and QA** tab in the developer console to run it in the portal frame with ads on ([build preview](https://docs.zokogames.com/getting-started/local-testing#the-build-preview-in-the-console)).

## Checklist

- [ ] Library added; the other portal's extension and calls removed.
- [ ] SDK script tag in a custom HTML5 template.
- [ ] `wasm-web`, no threads, Release.
- [ ] `zoko.init` first; the game plays when `is_available()` is false.
- [ ] Gameplay start and stop at the right moments.
- [ ] Ads resume in `finished` and `error`; rewards only on finish.
- [ ] Saves through `zoko.data`; scores on a board created in the console.
- [ ] Other portal's logos, links and sitelock removed.
- [ ] Tested on `localhost` and in **Preview and QA**.

## Next

- Full reference: [Defold](https://docs.zokogames.com/engines/defold).
- Before upload: [build validation](https://docs.zokogames.com/requirements/build-validation) and the [launch checklist](https://docs.zokogames.com/requirements/checklist).
- Upload and publish: [submission walkthrough](https://docs.zokogames.com/getting-started/submission-walkthrough).
