Skip to content

示例

@consenger/companion-sdk 的 browser-game integration 以这些形态为基准。

Vanilla browser game

html
<script src="./companion-sdk.js"></script>
<script>
  const companion = window.Consenger.companion;

  companion.context("round.current", {
    description: "Current round state visible to the player",
    read: () => ({
      round: 3,
      goal: "Pick the matching Japanese word",
      choices: ["みず", "そら", "ねこ"]
    })
  });

  companion.command("round.hint", {
    title: "Show a hint",
    inputSchema: { type: "object", properties: {}, additionalProperties: false },
    handler: () => {
      showHint();
      return { ok: true };
    }
  });
</script>

Phaser scene

ts
import { createCompanionSDK } from "@consenger/companion-sdk";

const companion = createCompanionSDK({ transport: "auto" });

export class QuizScene extends Phaser.Scene {
  create() {
    companion.context("scene.current", {
      description: "Current quiz scene",
      read: () => ({
        scene: this.scene.key,
        score: this.score,
        prompt: this.currentPrompt
      })
    });
  }
}

Godot Web export

Godot Web export 可以把 SDK browser bundle 放在 Project.html 旁边,再通过 JavaScript glue code 调用。gameplay state 应以 Godot 侧为 source of truth,companion.context() 只暴露小型、player-facing 的 snapshot。