Asobiai SDK

From Godot / Unity

Games made with an engine can use window.AIGame too when played on Asobiai. Engine scripts (GDScript, C#) can't see it directly, so call it through the engine's JavaScript bridge.

v2.7.0Check it from your game with window.AIGame?.version.Changelog

Godot 4

Use JavaScriptBridge. Add the script below as an Autoload named “Asobiai” and call it from anywhere. It does nothing outside the web.

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

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

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
# On game over (once)
Asobiai.submit_score(score, "desc", "pts")

# When a time attack is cleared
Asobiai.submit_score(clear_time, "asc", "sec")

# Attach the floor reached as a note (shown next to the number on the leaderboard)
Asobiai.submit_score(score, "desc", "pts", "B%dF" % floor)

# Trophy
Asobiai.unlock_achievement("first_clear")

To use the result (rank, personal best), pass a callback to the 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)
  • Export for Web with Thread Support off (exports that use threads don't run on Asobiai). Upload the exported folder, including the .pck and .wasm.
  • In Godot 3, use JavaScript instead of JavaScriptBridge and OS.has_feature("JavaScript") instead of OS.has_feature("web").

Unity

Write JavaScript functions in a .jslib plugin and call them from C# with DllImport. Nothing happens in the editor or on other platforms.

js
// Assets/Plugins/WebGL/AIGame.jslib
mergeInto(LibraryManager.library, {
  AIGameSubmitScore: function (score, board, order, label, note) {
    if (!window.AIGame) return;
    window.AIGame.submitScore(score, {
      board: UTF8ToString(board) || undefined,
      order: UTF8ToString(order) || "desc",
      label: UTF8ToString(label),
      note: UTF8ToString(note),
    });
  },
  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, string note);
    [DllImport("__Internal")] static extern void AIGameUnlockAchievement(string id);
#else
    static void AIGameSubmitScore(double score, string board, string order, string label, string note) { }
    static void AIGameUnlockAchievement(string id) { }
#endif

    // note: a short note for the score ("B23F", …; shown next to the number on the leaderboard)
    public static void SubmitScore(double score, string order = "desc", string label = "", string board = "", string note = "")
        => AIGameSubmitScore(score, board, order, label, note);

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

// Usage: AIGame.SubmitScore(score, label: "pts");  For a time: AIGame.SubmitScore(seconds, "asc", "sec");
  • Asobiai doesn't add Content-Encoding to compressed files, so in Player Settings → Publishing Settings either set Compression Format to Disabled or, with Gzip / Brotli, turn Decompression Fallback on.

Unreal Engine

From C++ in an HTML5 build, call it with Emscripten's EM_ASM.

cpp
// From C++ in an Unreal Engine HTML5 build
#include <emscripten.h>
EM_ASM({ window.AIGame && window.AIGame.submitScore($0); }, Score);

About saves

The engine's usual saves (Godot's user://, Unity's PlayerPrefs) stay in that browser. To save to the player's account and continue on other devices, call AIGame.save / load the same way (load returns a Promise, so take the result in a callback). Saves

Shown as a tool

Games made with Godot, Unity or Unreal Engine are detected from the uploaded files and the engine shows automatically under “Made with” on the game page. The game list can be filtered by it under “Tool”.

What to tell your AI

I'm publishing a Godot 4 web export on Asobiai. The game has a score, so add an Asobiai leaderboard: once on game over (or clear), when OS.has_feature("web"), call JavaScriptBridge.eval("window.AIGame?.submitScore(score, {\"order\":\"desc\",\"label\":\"pts\"})"). For lower-is-better values like times, use order "asc" and label "sec". Don't send every frame. Export with thread support turned off.