Asobiai SDK

Godot・Unity から使う

ゲームエンジンで作ったゲームも、Asobiai のページの中では window.AIGame が使えます。ただしエンジンのスクリプト(GDScript・C#)からは直接見えないので、エンジンの「JavaScript を呼ぶ仕組み」を通して呼びます。

v2.4.0ゲームの中からは window.AIGame?.version で確認できます。更新履歴

Godot 4

JavaScriptBridge で呼びます。下のスクリプトを自動読み込み(Autoload)に「Asobiai」という名前で入れておくと、どこからでも呼べます。Web 以外で動かしたときは何もしません。

gdscript
# asobiai.gd (自動読み込み = Autoload にしておくと、どこからでも Asobiai.submit_score(…) で呼べる)
extends Node

# スコアを送る (order: "desc" = 大きいほど上位 / "asc" = 小さいほど上位。label は単位)
func submit_score(score: float, order := "desc", label := "") -> void:
	if OS.has_feature("web"):
		JavaScriptBridge.eval("window.AIGame?.submitScore(%s, %s)" % [score, JSON.stringify({"order": order, "label": label})])

func unlock_achievement(id: String) -> void:
	if OS.has_feature("web"):
		JavaScriptBridge.eval("window.AIGame?.unlockAchievement(%s)" % JSON.stringify(id))

func tally(name: String, choice) -> void:
	if OS.has_feature("web"):
		JavaScriptBridge.eval("window.AIGame?.tally(%s, %s)" % [JSON.stringify(name), JSON.stringify(choice)])
gdscript
# ゲームオーバーのとき (1回だけ)
Asobiai.submit_score(score, "desc", "点")

# タイムアタックをクリアしたとき
Asobiai.submit_score(clear_time, "asc", "秒")

# トロフィー
Asobiai.unlock_achievement("first_clear")

結果(何位か・自己ベスト)を使いたいときは、Promise にコールバックを渡します。

gdscript
# 結果 ({ ok, rank, best } / { ok, reason }) を受け取る
var _on_score_cb = JavaScriptBridge.create_callback(_on_score)  # 変数に入れて持っておく

func submit_score_with_result(score: float) -> void:
	if not OS.has_feature("web") or not JavaScriptBridge.eval("!!window.AIGame"):
		return
	JavaScriptBridge.get_interface("AIGame").submitScore(score).then(_on_score_cb)

func _on_score(args: Array) -> void:
	var result = args[0]
	if result.ok:
		print("rank: ", result.rank, " best: ", result.best)
  • 書き出しは Web、Thread Support(スレッド)はオフにしてください(Asobiai ではスレッドを使う書き出しは動きません)。書き出したフォルダを、.pck と .wasm ごとアップロードします。
  • Godot 3 では JavaScriptBridge のかわりに JavaScript、OS.has_feature("web") のかわりに OS.has_feature("JavaScript") を使います。

Unity

.jslib のプラグインで JavaScript の関数を作り、C# から DllImport で呼びます。エディタやほかの環境では何もしません。

js
// Assets/Plugins/WebGL/AIGame.jslib
mergeInto(LibraryManager.library, {
  AIGameSubmitScore: function (score, board, order, label) {
    if (!window.AIGame) return;
    window.AIGame.submitScore(score, {
      board: UTF8ToString(board) || undefined,
      order: UTF8ToString(order) || "desc",
      label: UTF8ToString(label),
    });
  },
  AIGameUnlockAchievement: function (id) {
    if (window.AIGame) window.AIGame.unlockAchievement(UTF8ToString(id));
  },
});
csharp
// Assets/Scripts/AIGame.cs
using System.Runtime.InteropServices;

public static class AIGame
{
#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")] static extern void AIGameSubmitScore(double score, string board, string order, string label);
    [DllImport("__Internal")] static extern void AIGameUnlockAchievement(string id);
#else
    static void AIGameSubmitScore(double score, string board, string order, string label) { }
    static void AIGameUnlockAchievement(string id) { }
#endif

    public static void SubmitScore(double score, string order = "desc", string label = "", string board = "")
        => AIGameSubmitScore(score, board, order, label);

    public static void UnlockAchievement(string id) => AIGameUnlockAchievement(id);
}

// 使うとき: AIGame.SubmitScore(score, label: "点");  タイムなら AIGame.SubmitScore(seconds, "asc", "秒");
  • Asobiai は圧縮したファイルに Content-Encoding を付けないので、Player Settings の Publishing Settings で Compression Format を Disabled にするか、Gzip・Brotli のときは Decompression Fallback をオンにしてください。

Unreal Engine

HTML5 書き出しの C++ からは、Emscripten の EM_ASM で呼べます。

cpp
// Unreal Engine (HTML5 書き出し) の C++ から
#include <emscripten.h>
EM_ASM({ window.AIGame && window.AIGame.submitScore($0); }, Score);

セーブについて

エンジンのふつうのセーブ(Godot の user://、Unity の PlayerPrefs)も、そのブラウザの中には残ります。アカウントに保存してほかの端末で続けたいときは、同じように AIGame.save / load を呼んでください(load は Promise を返すので、結果はコールバックで受け取ります)。 セーブ・ロード

エンジンのタグ

Godot・Unity・Unreal Engine で作ったゲームには、アップロードしたファイルから見分けて「#Godot」などのタグが自動で付きます(自分で付けたタグの数には数えません)。

AIに伝えるときの文面

Godot 4 の Web 書き出しで Asobiai に公開します。スコアがあるので、Asobiai のランキングを付けてください。ゲームオーバー(またはクリア)のときに1回だけ、OS.has_feature("web") のときに JavaScriptBridge.eval("window.AIGame?.submitScore(スコア, {\"order\":\"desc\",\"label\":\"点\"})") を呼んでください。タイムのように小さいほど良いものは order を "asc" に、label を "秒" にしてください。毎フレームは送らないでください。書き出しはスレッドを使わない設定にしてください。