ActionsApi.sayAll(text)方法說明與使用範例

| Chrono Divide | 2 Reads

以下代碼節選自 node_modules/@chronodivide/game-api 的型別宣告檔案,定義了 ActionsApi 類的方法。其中第 23 行就是 sayAll 的宣告:

orderUnits(unitIds: number[], orderType: OrderType): void;
orderUnits(unitIds: number[], orderType: OrderType, targetUnit: number): void;
orderUnits(unitIds: number[], orderType: OrderType, rx: number, ry: number, onBridge?: boolean): void;
sayAll(text: string): void;
/**
 * Prints a persistent multiline debug string in the game client at a fixed position on the screen
 * ...
 */
setGlobalDebugText(text: string | undefined): void;

該方法在 bot 程式中被實際呼叫,用來在遊戲內廣播訊息給所有玩家:

118      this.logger.info(message);
119      if (sayInGame) {
120          const timestamp = this.getHumanTimestamp(this.gameApi);
121          this.actionsApi.sayAll(`${timestamp}: ${message}`);
122      }

功能說明
ActionsApi.sayAll(text) 用於讓機器人在遊戲中向所有玩家發送一條聊天訊息。它不會傳回任何值,只是將提供的 text 作為聊天內容傳遞給遊戲。通常用於紀錄關鍵事件或在除錯時向人類玩家顯示機器人狀態。


使用範例
假設我們想在遊戲開始時發送一句問候,可以在 onGameStart 中這樣撰寫:

import { GameApi } from "@chronodivide/game-api";

class MyBot extends Bot {
    override onGameStart(game: GameApi) {
        super.onGameStart(game);
        this.actionsApi.sayAll("Hello everyone, good luck!");
    }
}

執行時,這段程式碼會讓機器人在遊戲畫面的聊天框中顯示 “Hello everyone, good luck!”。若所有玩家都在線上,他們都能看到這條訊息。這可以用於除錯或簡單互動。

 

→返回《@chronodivide/game-api 使用教學與完整 API 對照表》

This article was last edited at