# Godot 3 {#godot-3}

แอดออน `zokogames` (`packages/sdk-engines/godot3/addons/zokogames`) รองรับการส่งออก HTML5 ของ **Godot 3** (เป้าหมายคือ 3.5 และ 3.6 LTS ส่วน 3.3 และ 3.4 น่าจะใช้ได้ และ 3.0 ถึง 3.2 ไม่รองรับ) มี API สาธารณะเหมือนกับ[แอดออน Godot 4](https://docs.zokogames.com/th/engines/godot) คือออโต้โหลด `ZokoGames` ตัวเดียวกัน ชื่อเมธอดเดียวกัน และซิกนัลเดียวกัน โปรเจกต์จึงย้ายไปมาระหว่างสองเวอร์ชันได้ เขียนด้วย GDScript 1 (ไม่มี `await` ไม่มีแลมบ์ดา) และเรียก JavaScript SDK ผ่านซิงเกิลตัน `JavaScript` โดยใช้บริดจ์ขนาดเล็ก (`window.ZokoBridge`) ที่กำหนดไว้ใน HTML เชลล์ของการส่งออก นอกเบราว์เซอร์ (ในตัวแก้ไข หรือรันบนเดสก์ท็อป) ตัวจำลองในตัวจะตอบแทน โค้ดเดียวกันจึงรันได้ทุกที่ ศัพท์อย่าง "midgame" และ "rewarded" อยู่ใน[อภิธานศัพท์](https://docs.zokogames.com/th/getting-started/glossary)

> [!NOTE]
> สถานะ (1.8.0): รันแบบ headless ในเอดิเตอร์ Godot 3.6.3 ทางการบน macOS แอดออน import ได้โดยไม่มีข้อผิดพลาดหรือคำเตือน (ถือว่าคำเตือนเป็นข้อผิดพลาด) การเปิดใช้ปลั๊กอินลงทะเบียน autoload ทุกเมธอดสาธารณะและซิกนัลทำงานตามเอกสารในโหมดซิมูเลเตอร์ และการส่งออก HTML5 มีบริดจ์รวมอยู่และรันในเบราว์เซอร์ได้ ยังไม่ได้ตรวจสอบ Godot 3.3 ถึง 3.5 และยังไม่ได้รันกับพอร์ทัลจริง หาก import ครั้งแรกล้มเหลว ให้ส่งเวอร์ชัน Godot และข้อความผิดพลาดที่ตรงตัวไปที่[การสนับสนุน](https://docs.zokogames.com/th/business/support)

บน Godot 4 แอดออนนี้จะไม่ยอมเริ่มทำงาน (`init()` ล้มเหลวด้วย `unsupportedEngine`) ให้ใช้[หน้า Godot 4](https://docs.zokogames.com/th/engines/godot) แทน

## ติดตั้ง {#install}

ดาวน์โหลดปลั๊กอิน Godot 3: [/sdk-engines/zokogames-godot3-1.8.1.zip](https://www.zokogames.com/sdk-engines/zokogames-godot3-1.8.1.zip) (ไฟล์ชุดเดียวกับโฟลเดอร์ในรีโพสิทอรี)

[/sdk-engines/godot3.zip](https://www.zokogames.com/sdk-engines/godot3.zip) เป็นเวอร์ชันล่าสุดเสมอ เช็กซัม: [`/sdk-engines/SHA256SUMS`](https://www.zokogames.com/sdk-engines/SHA256SUMS) ดัชนีของแพ็กเกจเอนจินทั้งหมดพร้อมเวอร์ชัน ขนาด และ SHA-256: [`/sdk-engines/engines.json`](https://www.zokogames.com/sdk-engines/engines.json) ซอร์สโค้ดเผยแพร่ที่ [github.com/zokogames/zokogames-godot3](https://github.com/zokogames/zokogames-godot3) ด้วย มีใน Godot Asset Library ด้วย (asset 5620): ค้นหา ZokoGames ในแท็บ AssetLib ของเอดิเตอร์ (Godot 3) หรือติดตั้งจากไฟล์ zip

1. แตกไฟล์ที่ดาวน์โหลด แล้วคัดลอกโฟลเดอร์ `addons/zokogames` ไปไว้ในโฟลเดอร์ `res://addons/` ของโปรเจกต์ (สร้าง `addons` หากยังไม่มี) ในนั้นมี `plugin.cfg`, `plugin.gd`, `zokogames.gd` (ออโต้โหลด), `zoko_banner.gd` และ `export/zokogames_shell.html`
2. **Project > Project Settings > Plugins**: เปิดใช้ **ZokoGames** ขั้นตอนนี้ลงทะเบียนออโต้โหลด `ZokoGames` (`res://addons/zokogames/zokogames.gd`) และการตั้งค่า `zokogames/simulator/*`
3. **Project > Export**: เพิ่มพรีเซ็ต **HTML5** แล้วตั้ง **Custom HTML Shell** (แท็บ Options > HTML) เป็น `res://addons/zokogames/export/zokogames_shell.html` เชลล์จะโหลด `https://sdk.zokogames.com/zokogames-sdk-v1.js` ก่อนเอนจิน สร้าง `window.ZokoBridge` และรายงานการดาวน์โหลดเอนจินไปที่แถบโหลดของพอร์ทัล หากใช้เชลล์ของคุณเอง ให้เพิ่มแท็ก script ใน `<head>` และคัดลอกบล็อกบริดจ์จากเชลล์ที่ให้มา
4. `yield(ZokoGames.init_async(), "completed")` หนึ่งครั้งตอนเริ่มต้น ก่อนการเรียกอื่นใด
5. ไม่บังคับ: เพิ่มโหนด `ZokoBanner` (`zoko_banner.gd`) ทับพื้นที่ที่คุณเว้นไว้สำหรับแบนเนอร์ได้ โฟลเดอร์ `examples/` ข้างแอดออนมี `zoko_demo.gd` และ `zoko_store.gd`

> [!IMPORTANT]
> หากไม่มีเชลล์แบบกำหนดเอง `window.ZokoBridge` จะไม่มีอยู่ แอดออนจะบันทึกคำเตือนและถอยไปใช้ตัวจำลอง เกมจึงรันได้ แต่ไม่มีโฆษณา ผู้ใช้ หรือการเซฟจริง

### ชุดเริ่มต้น {#starter-kit}

โปรเจกต์ Godot 3 สำเร็จรูปที่มีแอดออน เชลล์ และซีนตัวอย่างขนาดเล็ก: [/starter-kits/godot3.zip](https://www.zokogames.com/starter-kits/godot3.zip) ดูหน้า[ชุดเริ่มต้น](https://docs.zokogames.com/th/getting-started/starter-kits)

## ความแตกต่างจาก Godot 4 {#what-differs-from-godot-4}

| Godot 4 | Godot 3 |
| --- | --- |
| `await ZokoGames.get_user_async()` | `yield(ZokoGames.get_user_async(), "completed")` ซึ่งคืน Dictionary เดียวกัน |
| `callback: Callable` | `callback`: เป็น `FuncRef`, `funcref(self, "_on_done")` |
| `ZokoGames.ad_started.connect(f)` | `ZokoGames.connect("ad_started", self, "_on_ad_started")` |
| แอดออนหยุดทรีและปิดเสียงบัส Master ขณะโฆษณาเล่น และใช้การตั้งค่าปิดเสียงของพอร์ทัล (`pause_game_during_ads`, `mute_audio_during_ads`, `follow_portal_mute` ซึ่งเป็น `true` ทั้งหมด) | พร็อพเพอร์ตีและค่าเริ่มต้นเดียวกัน โหนดของคุณเองที่ต้องทำงานระหว่างโฆษณาต้องตั้ง `pause_mode = PAUSE_MODE_PROCESS` |
| `JavaScriptBridge` | `JavaScript` |

การเรียก `_async` คืนอ็อบเจกต์คำขอ (มี `id`, `done`, `result` และซิกนัล `completed(result)`) และ `yield(..., "completed")` ให้ Dictionary ผลลัพธ์ ทุกคำขอเสร็จในเฟรมถัดไป ดังนั้น `yield` จึงไม่พลาด

## สคริปต์ที่ใช้งานได้จริง {#a-working-script}

วางไว้ในสคริปต์บนโหนดในซีนแรกของคุณ (หรือในออโต้โหลดของคุณเอง) ครอบคลุมการเริ่มต้น โฆษณา midgame โฆษณา rewarded การเริ่มและหยุดการเล่น การเซฟและโหลด และการส่งคะแนนลีดเดอร์บอร์ด

```gdscript
extends Node

func _ready() -> void:
	pause_mode = PAUSE_MODE_PROCESS                            # keep running while an ad pauses the tree
	var res = yield(ZokoGames.init_async(), "completed")
	if not res.ok:
		push_warning("ZokoGames unavailable: %s" % res.error.code)
	# The addon pauses the tree and mutes Master during an ad, and follows the portal's mute setting
	# (pause_game_during_ads, mute_audio_during_ads, follow_portal_mute): no ad or mute handlers needed.
	var level = int(ZokoGames.data_get_item("level", "1"))     # load: synchronous Strings
	start_game(level)

# Call when the player takes control, and when play stops.
func on_play_start() -> void:
	if ZokoGames.is_available():
		ZokoGames.gameplay_start()
func on_play_stop() -> void:
	if ZokoGames.is_available():
		ZokoGames.gameplay_stop()

func show_midgame() -> void:
	var res = yield(ZokoGames.request_ad_async("midgame"), "completed")   # completes after the ad is over
	if not res.ok:
		print("no ad: ", res.error.code)                       # adCooldown, unfilled, adblock ...

func on_reward_button() -> void:
	var res = yield(ZokoGames.request_ad_async("rewarded"), "completed")
	if res.ok:
		grant_reward()                                         # reward only after the ad finished

func save_progress(level: int) -> void:
	var r = ZokoGames.data_set_item("level", str(level))       # synchronous, uploaded in the background
	if not r.ok:
		push_warning(r.error.code)                             # for example dataLimitExceeded

func submit_score(score: float) -> void:
	var user = yield(ZokoGames.get_user_async(), "completed")
	if not user.ok or user.result == null:
		return                                                 # guests are not ranked
	var r = yield(ZokoGames.submit_score_async("", score), "completed")   # "" works only on a board WITHOUT an encryption key
	if not r.ok:
		print(r.error.code)

func start_game(_level: int) -> void:
	pass
func grant_reward() -> void:
	pass
```

## คะแนนลีดเดอร์บอร์ดและการเข้ารหัส {#leaderboard-scores-and-encryption}

บอร์ดที่**มี**คีย์เข้ารหัสจะนับเฉพาะเพย์โหลดที่เข้ารหัสถูกต้อง ([ลีดเดอร์บอร์ด](https://docs.zokogames.com/th/sdk/leaderboards#client-submission)) ตั้งแต่ปลั๊กอิน 1.8.0 `encrypt_score_async(score, KEY)` จะผนึกคะแนนด้วยคีย์บอร์ดของคุณและคืนเพย์โหลดสำหรับ `submit_score_async` ส่วน `""` ใช้ได้เฉพาะบอร์ดที่ไม่มีคีย์

```gdscript
const BOARD_KEY = "paste-your-board-key-here"

func submit_secure_score(score: float) -> void:
	var sealed = yield(ZokoGames.encrypt_score_async(score, BOARD_KEY), "completed")
	if not sealed.ok:
		print(sealed.error.code)                               # userNotAuthenticated for guests, invalidArgument for a bad score or key
		return
	var r = yield(ZokoGames.submit_score_async(sealed.result, score), "completed")
	if not r.ok:
		print(r.error.code)
```

อาร์กิวเมนต์ที่สามที่ไม่บังคับ `earned_at` (มิลลิวินาทีแบบ Unix ค่าเริ่มต้น 0 สำหรับการเล่นสด) ใช้ผนึกสำเนาของคะแนนที่ส่งล่าช้า คีย์ที่อยู่ในบิลด์กันได้แค่การแก้ไขแบบลวกๆ ไม่ใช่ผู้เล่นที่ตั้งใจจริง หากเดิมพันสูง ให้คำนวณคะแนนบนเซิร์ฟเวอร์ของคุณและใช้ API ฝั่งเซิร์ฟเวอร์

## การเรียกทำงานอย่างไร {#how-calls-work}

<!-- search: headings-only -->

ชื่อ GDScript เป็น snake_case ทุกเมธอดที่คุยกับพอร์ทัลมีสองรูปแบบ:

| รูปแบบ | ตัวอย่าง | ใช้กับ |
| --- | --- | --- |
| คอลแบ็ก | `ZokoGames.get_user(callback)` | การเรียกแบบยิงแล้วลืม `FuncRef` เป็นทางเลือกและอยู่ท้ายสุด ยกเว้นใน `store_get_catalog_items(callback, limit, offset)` และ `store_get_player_inventory(callback, limit, offset)` ซึ่งอยู่ก่อน และใน `get_user_token(signing_key_type, callback)` |
| คำขอ | `yield(ZokoGames.get_user_async(), "completed")` | โค้ดเรียงบรรทัดตรงๆ ชื่อลงท้ายด้วย `_async` |

ทั้งสองส่ง Dictionary คือ `{ "ok": true, "result": <value> }` หรือ `{ "ok": false, "error": { "code": "...", "message": "..." } }` (`request_ad` รับ Dictionary ของ FuncRef แทน) ทุกเมธอดอยู่บนออโต้โหลดโดยตรง ไม่มีอ็อบเจกต์ย่อย `ad.`, `game.` หรือ `user.`

| JavaScript | GDScript |
| --- | --- |
| `SDK.init()` | `yield(ZokoGames.init_async(), "completed")` หรือ `ZokoGames.init(callback)`; ซิกนัล `initialized(ok)` |
| `SDK.environment` | `ZokoGames.environment` (`"local"`, `"zokogames"` หรือ `"disabled"`), `ZokoGames.is_available()`, `ZokoGames.is_simulated`, `ZokoGames.is_qa_tool`, `ZokoGames.VERSION` |
| `SDK.ad.requestAd(type, cbs)` | `ZokoGames.request_ad(type, {"ad_started": f, "ad_finished": f, "ad_error": f})` (FuncRef) หรือ `yield(ZokoGames.request_ad_async(type), "completed")` |
| `SDK.ad.hasAdblock()` | `ZokoGames.has_adblock(callback)` หรือ `yield(ZokoGames.has_adblock_async(), "completed")` |
| `SDK.banner.requestBanner(...)` | `yield(ZokoGames.request_banner_async(id, width, height, window_rect), "completed")` |
| `SDK.banner.requestResponsiveBanner(id)` | `yield(ZokoGames.request_responsive_banner_async(id, window_rect), "completed")` |
| `SDK.banner.clearBanner(id)` / `clearAllBanners()` | `ZokoGames.clear_banner(id)` / `ZokoGames.clear_all_banners()` |
| `SDK.banner.hideAllBanners()` / `showAllBanners()` (1.8.0) | `ZokoGames.hide_all_banners()` / `ZokoGames.show_all_banners()` (และ `_async`) |
| `SDK.game.gameplayStart()` / `gameplayStop()` | `ZokoGames.gameplay_start()` / `ZokoGames.gameplay_stop()` |
| `SDK.game.loadingStart()` / `loadingStop()` / `loadingProgress(n)` | `ZokoGames.loading_start()` / `loading_stop()` / `loading_progress(n)` |
| `SDK.game.happytime()` | `ZokoGames.happytime()` |
| `SDK.game.reportGameCompletedPercentage(n)` | `ZokoGames.report_game_completed_percentage(n)` |
| `SDK.game.setGameContext(obj)` / `clearGameContext()` | `ZokoGames.set_game_context(dict)` / `ZokoGames.clear_game_context()` |
| `SDK.game.updateRoom(u)` / `leftRoom()` | `ZokoGames.update_room(dict)` / `ZokoGames.left_room()` |
| `SDK.game.inviteLink(p)` / `getInviteParam(k)` | `yield(ZokoGames.invite_link_async(dict), "completed")` / `ZokoGames.get_invite_param(key)` |
| `SDK.game.showInviteButton(p)` / `hideInviteButton()` | `yield(ZokoGames.show_invite_button_async(dict), "completed")` / `ZokoGames.hide_invite_button()` |
| `SDK.game.settings` | `ZokoGames.settings` (`disableChat`, `muteAudio`) ซิกนัล `settings_changed` |
| `SDK.game.isOffline`, ตัวฟังสถานะออฟไลน์ | `ZokoGames.is_offline` ซิกนัล `offline_changed(offline)` |
| `SDK.game.isInstantMultiplayer` / `inviteParams` | `ZokoGames.is_instant_multiplayer` (ชื่อแทน `is_instant_join`) / `ZokoGames.invite_params` ซิกนัล `join_room(params)` |
| `SDK.user.isUserAccountAvailable` / `systemInfo` | `ZokoGames.is_user_account_available` / `ZokoGames.system_info` |
| `SDK.user.getUser()` | `yield(ZokoGames.get_user_async(), "completed")` |
| `SDK.user.getUserToken(opts)` | `yield(ZokoGames.get_user_token_async(signing_key_type), "completed")` |
| `SDK.user.showAuthPrompt()` | `yield(ZokoGames.show_auth_prompt_async(), "completed")` ซิกนัล `auth_changed(user)` |
| `SDK.user.showAccountLinkPrompt()` | `yield(ZokoGames.show_account_link_prompt_async(), "completed")` |
| `SDK.user.listFriends(opts)` | `yield(ZokoGames.list_friends_async(page, size), "completed")` |
| `SDK.user.submitScore(opts)` | `yield(ZokoGames.submit_score_async(encrypted_score, score), "completed")` |
| `SDK.user.encryptScore(score, key)` (1.8.0) | `yield(ZokoGames.encrypt_score_async(score, key), "completed")` |
| `SDK.game.gameId` (1.8.0) | `ZokoGames.game_id` / `get_game_id()` |
| `SDK.data.mirrorIndexedDB()` (1.8.0) | ตัวเลือกใน HTML เชลล์ `window.ZOKO_MIRROR_INDEXEDDB = true` (Head Include); `ZokoGames.stop_mirror_indexed_db()` |
| `SDK.data.getItem(k)` / `setItem(k, v)` | `ZokoGames.data_get_item(key, default)` / `ZokoGames.data_set_item(key, value)`; รวมถึง `data_has_key`, `data_remove_item`, `data_clear` (ซิงโครนัสทั้งหมด) |
| `SDK.store.*` | `store_get_catalog_items_async`, `store_get_player_inventory_async`, `store_buy_item_async(item_id)`, `store_consume_item_async(sku, qty)`, `store_get_purchase_token_async()`, `is_store_available`, `store_set_sandbox(bool)` (ซิงโครนัส); ซิกนัล `purchase_completed(item_id)`, `purchase_failed(error)` |
| `SDK.achievements.list()` / `unlock(key)` / `onUnlock` | `yield(ZokoGames.achievements_list_async(), "completed")` / `achievements_unlock_async(key)` ซิกนัล `achievement_unlocked(achievement)`; ดู[ความสำเร็จ](https://docs.zokogames.com/th/sdk/achievements) |
| `SDK.analytics.trackOrder(p, o)` | `yield(ZokoGames.track_order_async(provider, order), "completed")` |
| `SDK.analytics.track(name, params)` (1.7.0) | `ZokoGames.track(name, params)` / `track_async(name, params)` |


ซิกนัลบนออโต้โหลด: `initialized`, `ad_started`, `ad_finished`, `ad_error(error)`, `auth_changed`, `settings_changed`, `offline_changed`, `join_room`, `purchase_completed`, `purchase_failed` เชื่อมด้วย `ZokoGames.connect("ad_finished", self, "_on_ad_finished")` รหัสข้อผิดพลาดอยู่ใน `ZokoGames.ERROR_CODES` (Dictionary ที่ใช้โมดูลเป็นคีย์ เช่น `ZokoGames.ERROR_CODES.ad`) และ `ZokoGames.has_error_code(res, "purchaseCancelled")` ทดสอบผลลัพธ์ `unsupportedEngine` เป็นรหัสเพิ่มเติมในแอดออนนี้

**โหมดเด็ก.** `ZokoGames.settings.get("kidsMode", false)` เป็น true เมื่อเกมทำงานในโหมดเด็ก (เกมที่มีแฟล็กสำหรับเด็ก หรือผู้เล่นที่อยู่ในโหมดเด็ก) เมื่อนั้นให้ซ่อนแชต ห้อง คำชวนให้ลงชื่อเข้าใช้ และหน้าจอร้านค้า และให้เคารพ `ZokoGames.settings.disableChat` ต่อไปด้วย หากต้องการทดสอบ ให้เปิด `zokogames/simulator/kids_mode` ดู[โหมดเด็ก](https://docs.zokogames.com/th/guides/kids-mode)

## เซฟบนคลาวด์สำหรับ user:// (การสะท้อน IndexedDB) {#cloud-save-for-user-indexeddb-mirror}

การส่งออก Web เก็บไฟล์ `user://` ไว้ใน IndexedDB ของเบราว์เซอร์ (`/userfs`) หากต้องการเซฟไฟล์เหล่านั้นลงคลาวด์เซฟของผู้เล่น ให้เพิ่มบรรทัดนี้ใน **Export > HTML > Head Include**:

```html
<script>window.ZOKO_MIRROR_INDEXEDDB = true;</script>
```

เชลล์จะคืนค่าสำเนาจากคลาวด์ก่อนเอนจินเริ่ม และอัปโหลดการเขียนครั้งหลังภายในไม่กี่วินาที ข้อมูลที่สะท้อนต้องไม่เกิน 1 MiB และต้องเปิดใช้โมดูลข้อมูลสำหรับเกม `ZokoGames.stop_mirror_indexed_db()` หยุดการสะท้อน `ZokoGames.mirror_local_storage({"prefix": "save_"})` เป็นการสะท้อนแยกต่างหากสำหรับ `localStorage` ฝั่ง JavaScript และมองไม่เห็นไฟล์ `user://`

## ช่องข้อมูลห้อง {#room-data-channel}

ช่องข้อมูลห้อง (SDK 1.3.0) ให้ผู้เล่นในห้องเดียวกันแลกเปลี่ยนข้อความ JSON ขนาดเล็ก และใช้สถานะเล็กๆ ร่วมกันที่เฉพาะโฮสต์เขียนได้ ใช้สำหรับข้อมูลเกม ไม่ใช่แชตข้อความ ขีดจำกัดและรหัสข้อผิดพลาดเหมือนกับ Godot 4: ดู[โมดูลเกม](https://docs.zokogames.com/th/sdk/game#room-data-channel)และ[คู่มือการเชิญผู้เล่นหลายคน](https://docs.zokogames.com/th/guides/multiplayer-invites#6-exchange-game-data-in-the-room) เพย์โหลดข้ามบริดจ์เป็น JSON (ตัวเลขกลับมาเป็น `float` ให้แปลงด้วย `int()`)

| JavaScript | Godot |
| --- | --- |
| `room.send(data, { to })` | `ZokoGames.room_send(data, to, cb)` / `room_send_async(data, to)` / `room_send_to(member_id, data, cb)` |
| `room.onMessage(cb)` | ซิกนัล `room_message(message)` (`from`, `data`, `direct`, `seq`, `ts`) |
| `room.setState(patch)` | `room_set_state(patch, cb)` / `room_set_state_async(patch)` (ค่า `null` จะลบคีย์) |
| `room.getState()`, `room.onState(cb)` | `room_get_state()`, `room_get_state_value(key, default)`; ซิกนัล `room_state(state, change)` |
| `room.members()`, `room.onMembers(cb)` | `room_get_members()`; ซิกนัล `room_members(members)` |
| `room.isHost()`, `room.onHostChange(cb)`, `room.selfId` | `room_is_host()`; ซิกนัล `room_host_changed(host_id, is_host)`; `room_self_id()` |
| `room.addLeftListener(cb)` (1.7.0) | ซิกนัล `room_left(reason, room_id)` |
| `room.onError(cb)` | ซิกนัล `room_error(error)` |

```gdscript
extends Node

func _ready() -> void:
	yield(ZokoGames.init_async(), "completed")
	ZokoGames.connect("room_message", self, "_on_message")
	ZokoGames.connect("room_host_changed", self, "_on_host_changed")
	yield(ZokoGames.update_room_async({"roomId": "match-1", "isJoinable": true}), "completed")

func _on_host_changed(_host_id, is_host) -> void:
	if is_host:
		ZokoGames.room_set_state({"turn": "X"})

func _on_message(message) -> void:
	print(message.from, " played cell ", int(message.data.cell))

func play_cell(cell: int) -> void:
	yield(ZokoGames.room_send_async({"cell": cell, "mark": "X"}), "completed")
```

## แบนเนอร์ {#banners}

สี่เหลี่ยมของแบนเนอร์อยู่ในพิกเซลของหน้าต่าง วางโหนด `ZokoBanner` (เป็น `Control`) ตรงที่ต้องการให้แบนเนอร์อยู่ โหนดจะรายงานสี่เหลี่ยมบนหน้าจอของตัวเอง (คำนึงถึงโหมดสเตรตช์) ขอแบนเนอร์ใน `_ready` และล้างตัวเองเมื่อออกจากทรี

| พร็อพเพอร์ตี | ความหมาย |
| --- | --- |
| `container_id` | รหัสแบนเนอร์ ค่าเริ่มต้น `zoko-banner` |
| `size_mode` | `responsive` (ขนาดใหญ่สุดที่พอดี) หรือหนึ่งใน `728x90`, `300x250`, `320x50`, `468x60`, `320x100` |
| `request_on_ready` | ขออัตโนมัติใน `_ready` |
| `show_simulator_placeholder` | วาดสี่เหลี่ยมตัวแทนขณะที่ตัวจำลองตอบ |

ทำเอง: `yield(ZokoGames.request_responsive_banner_async("menu", $BannerSlot.get_global_rect()), "completed")` ขอแบนเนอร์เฉพาะบนหน้าจอเมนูเท่านั้น

## การซื้อ {#purchases}

```gdscript
func buy_first_item() -> void:
	if not ZokoGames.is_store_available:
		return
	var catalog = yield(ZokoGames.store_get_catalog_items_async(), "completed")
	if not catalog.ok or catalog.result.empty():
		return
	var bought = yield(ZokoGames.store_buy_item_async(catalog.result[0].sku), "completed")
	if ZokoGames.has_error_code(bought, "purchaseCancelled"):
		return
	var token = yield(ZokoGames.store_get_purchase_token_async(), "completed")   # send to your server and verify there
```

ตรวจสอบความเป็นเจ้าของบนเซิร์ฟเวอร์ของคุณ (ดู[การซื้อในเกม](https://docs.zokogames.com/th/sdk/purchases)) `examples/zoko_store.gd` คือขั้นตอนทั้งหมดแบบสมบูรณ์

## การตั้งค่าส่งออก HTML5 ที่สำคัญ {#html5-export-settings-that-matter}

<!-- search: headings-only -->

- **Custom HTML Shell**: `res://addons/zokogames/export/zokogames_shell.html` ตามด้านบน แคนวาสเต็มเฟรม จึงใช้ **Canvas Resize Policy** เป็น `Adaptive` และใน Project Settings > Display > Window > Stretch ให้ใช้ **Mode** เป็น `2d` หรือ `viewport` โดย aspect เป็น `expand` หรือ `keep`
- **เธรด** Godot 3 ไม่มีการส่งออกเว็บแบบมัลติเธรด จึงไม่มีอะไรให้เอาเครื่องหมายติ๊กออก หน้าเกมไม่ได้แยก cross-origin ดังนั้นหากบิลด์ของคุณแสดงตัวเลือก `SharedArrayBuffer` ให้ปิดไว้
- **GDNative** ปิด ไลบรารีเนทีฟไม่ทำงานในบิลด์เว็บ เว้นแต่คุณส่งไลบรารีที่ใช้กับเว็บได้ **Export With Debug**: ปิดสำหรับบิลด์ที่อัปโหลด
- **Focus Canvas on Start**: เปิด **Experimental Virtual Keyboard**: เปิดหากเกมมีการป้อนข้อความบนโทรศัพท์
- **ชื่อไฟล์** ส่งออกเป็นไฟล์ชื่อ `index.html`: พอร์ทัลต้องการ `index.html` ที่รากของไฟล์เก็บถาวร และการส่งออกตั้งชื่อไฟล์อื่นตามชื่อนั้น: `index.js`, `index.wasm`, `index.pck` **อย่า**เปลี่ยนชื่อเป็น `.br` หรือ `.gz`: เอนจินขอชื่อธรรมดา
- **การบีบอัดเท็กซ์เจอร์** เปิดการบีบอัด VRAM ทั้งสำหรับเดสก์ท็อป (S3TC) และมือถือ (ETC2) เพื่อให้ทุกเบราว์เซอร์มีฟอร์แมตที่ใช้ได้
- **ขนาด** ใช้เทมเพลตส่งออกแบบกำหนดเองที่ปิดโมดูลที่ไม่ใช้ และเก็บ `.pck` ให้เล็ก ขีดจำกัด: 1,500 ไฟล์ 250 MB เมื่อแตกไฟล์ ดาวน์โหลดเริ่มต้นราว 50 MB (20 MB สำหรับมือถือ)
- **หน่วยความจำ** เบราว์เซอร์มือถือมีฮีปเล็ก โหลดด่านเป็นช่วงๆ ด้วย `ResourceLoader.load_interactive` แทนการโหลดเท็กซ์เจอร์ทั้งหมดล่วงหน้า และจำกัดความละเอียดบนโทรศัพท์ ดู[มือถือ](https://docs.zokogames.com/th/guides/mobile)
- **การปลดล็อกเสียง** เบราว์เซอร์จะระงับเสียงไว้จนกว่าจะคลิกหรือกดปุ่มครั้งแรก ให้แสดงหน้าจอ "คลิกเพื่อเล่น" และเริ่มเพลงจากอินพุตนั้น ไม่ใช่ใน `_ready`
- **เต็มจอ** ใช้ปุ่มเต็มจอบนแถบเฟรมของพอร์ทัล `OS.window_fullscreen = true` จะทำเต็มจอเฉพาะแคนวาสของคุณ อย่าเพิ่มปุ่มที่แย่งหน้าที่กัน
- **การโหลด** เชลล์รายงานการดาวน์โหลดเอนจินและเรียก `loadingStop` เมื่อเอนจินเริ่มแล้ว สำหรับหน้าจอโหลดในเกมที่นานกว่านั้น ให้เรียก `ZokoGames.loading_start()`, `loading_progress(percent)` และ `loading_stop()` เอง

อัปโหลด: **Project > Export** เลือกพรีเซ็ต **HTML5** คลิก **Export Project...** และบันทึกลงในโฟลเดอร์ว่างด้วยชื่อไฟล์ `index.html` บีบอัด zip เนื้อหาของโฟลเดอร์เพื่อให้ `index.html` อยู่ที่รากของไฟล์ อัปโหลดในคอนโซลนักพัฒนา และรัน[เครื่องมือพรีวิว](https://www.zokogames.com/th/preview)ก่อน

## กฎของโฆษณา {#ad-rules}

ไม่มี preroll: เกมของคุณขอโฆษณาก่อนเกมเริ่มไม่ได้ Midgame เป็น interstitial ชนิดเดียว ดังนั้น interstitial หรือคอมเมอร์เชียลเบรกจากแพลตฟอร์มอื่นจึงกลายเป็นโฆษณา midgame (`request_ad("midgame")`) ชนิดอื่นมีเพียง `rewarded` ให้เรียก `ZokoGames.gameplay_start()` เมื่อผู้เล่นได้ควบคุม: โฆษณา midgame แรกต้องใช้การเรียกนี้ (หรือเวลาที่หน้าเพจมองเห็นครบ 30 วินาที) และในโหมดในเครื่อง คำขอ midgame ก่อนหน้านั้นจะล้มเหลวด้วย `adCooldown` โฆษณา rewarded ไม่ถูกหน่วงด้วยเรื่องนี้ รายละเอียด: [เอกสารอ้างอิงโฆษณาวิดีโอ](https://docs.zokogames.com/th/sdk/ads#gameplay-first) และ[คู่มือการสร้างรายได้จากโฆษณา](https://docs.zokogames.com/th/guides/ad-monetisation)

## ทดสอบในเครื่อง {#test-locally}

- **ในตัวแก้ไขหรือการรันบนเดสก์ท็อป** `ZokoGames.is_simulated` เป็นจริง ปรับแต่งตัวจำลองได้ที่ **Project Settings > zokogames/simulator**: `ad_behaviour` (`fill`, `no_fill`, `error`), `ad_duration_seconds`, `adblock`, `disabled`, `account_available`, `logged_in`, `username`, `user_id`, `friend_count`, `mute_audio`, `disable_chat`, `offline`, `store_available` และ `verbose` เซฟบนคลาวด์อยู่ใน `user://zoko_sim_data.json` คลังจำลองอยู่ใน `user://zoko_sim_inventory.json` ตัวจำลองไม่บังคับคูลดาวน์ midgame สามนาที
- **การส่งออก HTML5 บน `localhost`** เสิร์ฟโฟลเดอร์ส่งออกจากเว็บเซิร์ฟเวอร์ในเครื่อง (การเปิด `index.html` จากดิสก์ใช้กับ WebAssembly ไม่ได้) บริดจ์มีอยู่ `is_simulated` จึงเป็นเท็จ และ JavaScript SDK รัน[โหมดโลคอล](https://docs.zokogames.com/th/getting-started/local-testing)ของตัวเอง
- **จากนั้น** เปิด[เครื่องมือพรีวิว](https://www.zokogames.com/th/preview)ด้วยที่อยู่ในเครื่องของคุณ แล้วดูเช็กลิสต์ติ๊กทีละข้อ

## ข้อควรระวังที่พบบ่อย {#known-pitfalls}

<!-- search: headings-only -->

- **`sdkNotLoaded`** หน้าไม่มีแท็ก script ของ SDK ให้ใช้เชลล์แบบกำหนดเอง
- **ไม่มีออโต้โหลด `ZokoGames`** ยังไม่ได้เปิดใช้ปลั๊กอิน
- **การเรียกล้มเหลวด้วย `sdkNotInitialized`** ต้อง `yield(ZokoGames.init_async(), "completed")` ก่อน
- **`unsupportedEngine`** คุณกำลังรันแอดออนนี้บน Godot 4 ให้ใช้แอดออน Godot 4
- **`yield` ไม่เคยกลับมา** `yield` ในฟังก์ชันที่ถูกปลดปล่อยก่อน (เช่นเปลี่ยนซีน) จะถูกทิ้ง ให้เก็บโค้ดที่รออยู่ในโหนดที่ยังคงอยู่ หรือใช้รูปแบบคอลแบ็ก
- **ใช้ได้ในตัวแก้ไข แต่ไม่ใช้ในเบราว์เซอร์** เปิดคอนโซลเบราว์เซอร์และตรวจสอบว่ามี `window.ZokoBridge` (เชลล์เป็นผู้กำหนด)
- **เกมค้างหยุดชั่วคราวหรือถูกปิดเสียงหลังโฆษณา** มีบางอย่างอื่นเปลี่ยน `get_tree().paused` หรือบัส Master ระหว่างโฆษณา ให้ตั้ง `pause_game_during_ads` หรือ `mute_audio_during_ads` เป็น `false` แล้วจัดการ `ad_started`, `ad_finished` และ `ad_error` เอง อย่าใช้ `CONNECT_ONESHOT` กับ `ad_started`: เมื่อโฆษณาล้มเหลว ซิกนัลจะไม่ทำงาน
- **แบนเนอร์ไม่แสดงเลย** โหนด `ZokoBanner` ต้องมีขนาดและต้องมองเห็นได้
- **เซฟ `user://` ไม่อยู่บนคลาวด์** ใช้ `data_set_item` หรือการสะท้อน IndexedDB ข้างต้น

## รายงานแชทและภาพหน้าจอ {#chat-reports-and-screenshots}

`ZokoGames.report_chat({"reportedName": "Trixie", "message": "buy gold", "reason": "spam"}, callback)` (หรือ `report_chat_async`) ส่งรายงานแชท `ZokoGames.set_screenshot_provider(funcref)` แทนที่การจับภาพ viewport เริ่มต้น (ลงทะเบียนตอนเริ่มต้นหลัง `frame_post_draw`) และ `clear_screenshot_provider()` เอาของคุณออก ตัวจำลองในเอดิเตอร์รองรับทั้งสองอย่าง

## อีเวนต์ที่กำหนดเอง {#custom-events}

```gdscript
ZokoGames.track("level_complete", {"level": 3, "value": 1250})   # value is summed per event name
```

ไม่ต้องรอผล การเรียกจะไม่ throw พอร์ทัลรวมค่า `value` ที่เป็นตัวเลขตามชื่ออีเวนต์ และแสดงจำนวนครั้งที่เรียก ผู้เล่น และผลรวมใต้ **อีเวนต์ของเกมคุณเอง** ในคอนโซลนักพัฒนา (ต้องได้รับความยินยอมด้านการวิเคราะห์จากผู้เล่น ดู [การวิเคราะห์](https://docs.zokogames.com/th/sdk/analytics#custom-events-track)) ชื่อและคีย์ต้องตรงกับ `[a-z][a-z0-9_]{0,39}` และมีพารามิเตอร์ได้สูงสุด 10 รายการ ข้อผิดพลาดที่ไม่ถูกจับ SDK รายงานเอง จึงไม่ต้องเรียกอะไรสำหรับกรณีนั้น

## ถัดไป {#next}

- ตรวจสอบบิลด์ของคุณตาม[กฎการตรวจสอบบิลด์](https://docs.zokogames.com/th/requirements/build-validation)และ[เช็กลิสต์การเปิดตัว](https://docs.zokogames.com/th/requirements/checklist)
- อัปโหลดและเผยแพร่: [ขั้นตอนการส่งเกม](https://docs.zokogames.com/th/getting-started/submission-walkthrough)
- โปรเจกต์ Godot 4: [Godot](https://docs.zokogames.com/th/engines/godot) เอนจินอื่นและกฎที่ใช้ร่วมกัน: [ภาพรวมเอนจิน](https://docs.zokogames.com/th/engines/overview)
