KoolGames SDK
Save progress, report loading and gameplay, and get ready for ads with one small JavaScript file.
The SDK is optional. Add it if your game saves progress or you want it ready for future KoolGames features.
Add it to your game
Copy the file next to your game's `index.html`, then load it before your game's own code:
<script src="koolgames-sdk.js"></script>Call `init` once when the game starts:
KoolGames.init().then(function (result) {
console.log("Running on KoolGames:", result.onKoolGames);
});What it can do
| Call | What it does |
|---|---|
| KoolGames.init() | Starts the SDK. Call it once. |
| KoolGames.saveProgress(data, name) | Saves anything JSON can hold, up to 256 KB. The name is optional. Resolves to true or false. |
| KoolGames.loadProgress(name) | Resolves to the saved data, or null if there isn't any. |
| KoolGames.gameLoadingStart() | Your game has started loading. |
| KoolGames.gameLoadingFinished() | Your game has finished loading. |
| KoolGames.gameplayStart() | The player is now playing. |
| KoolGames.gameplayStop() | Play has paused, such as on a menu or game over screen. |
| KoolGames.submitScore(score) | Sends a whole number to your game's leaderboard. Only the player's best is kept. Resolves to saved and best. |
| KoolGames.unlockAchievement(id, name) | Unlocks an achievement once for this player. The name is shown to them, up to 40 characters. Resolves to unlocked and isNew. |
| KoolGames.onMuteChange(function) | Runs your function with true or false when the player presses the site's sound button. |
| KoolGames.showInterstitialAd() | Placeholder. Ads aren't live, so it resolves to shown: false straight away. |
| KoolGames.showRewardedAd() | Placeholder. Ads aren't live, so it resolves to shown: false and rewarded: false straight away. |
Never hold up your game waiting for an ad, and don't give a reward unless `rewarded` is true.
Where saves are kept
Saves are kept on the player's device, separately for each game. On KoolGames the site stores them. Outside KoolGames, such as when you test on your own computer, the SDK uses the browser's storage, so your code is the same in both places. Clearing browser data removes saves, so tell players if progress really matters.
Saving progress to player accounts will be added when KoolGames accounts open. You won't need to change your code.
HTML5
await KoolGames.init();
KoolGames.gameLoadingStart();
const save = await KoolGames.loadProgress();
const level = save ? save.level : 1;
KoolGames.gameLoadingFinished();
KoolGames.gameplayStart();
function onLevelComplete(next) {
KoolGames.saveProgress({ level: next });
}Unity
Add `koolgames-sdk.js` to your WebGL template and load it in the template's `index.html`. Then create `Assets/Plugins/WebGL/KoolGames.jslib`:
mergeInto(LibraryManager.library, {
KG_Save: function (json) {
KoolGames.saveProgress(JSON.parse(UTF8ToString(json)));
},
KG_Load: function (objectName) {
var target = UTF8ToString(objectName);
KoolGames.loadProgress().then(function (data) {
SendMessage(target, "OnSaveLoaded", JSON.stringify(data));
});
}
});Call it from C#. Attach this to a GameObject, and its name is used to send the save back:
using System.Runtime.InteropServices;
using UnityEngine;
public class KoolGamesSave : MonoBehaviour {
[DllImport("__Internal")] static extern void KG_Save(string json);
[DllImport("__Internal")] static extern void KG_Load(string objectName);
public void Save(string json) { KG_Save(json); }
public void Load() { KG_Load(gameObject.name); }
public void OnSaveLoaded(string json) { Debug.Log(json); }
}Godot
Load the SDK by adding the script tag to Head Include in your Web export preset, then use `JavaScriptBridge`:
var _on_loaded = JavaScriptBridge.create_callback(_loaded)
func save_game(data: Dictionary) -> void:
var sdk = JavaScriptBridge.get_interface("KoolGames")
var parsed = JavaScriptBridge.get_interface("JSON").parse(JSON.stringify(data))
sdk.saveProgress(parsed)
func load_game() -> void:
JavaScriptBridge.get_interface("KoolGames").loadProgress().then(_on_loaded)
func _loaded(args) -> void:
print(JavaScriptBridge.get_interface("JSON").stringify(args[0]))Other engines
Each of these can run JavaScript, so load `koolgames-sdk.js` in your exported `index.html` and call the same functions.
| Engine | Where to call the SDK |
|---|---|
| Defold | html5.run("KoolGames.gameplayStart()") in Lua |
| GameMaker | An extension with a JavaScript file that calls KoolGames |
| GDevelop | A JavaScript code event |
| Cocos | Directly in your TypeScript or JavaScript, using window.KoolGames |
| PlayCanvas | Add the file as an external script, then call it from a script |
| Unreal | From a web build, through a JavaScript call in your project's web template |
| Construct | A script file or "Run JavaScript" in the Browser object |
| LayaAir | Directly in your TypeScript or JavaScript, using window.KoolGames |
| iframe | Add the script tag to the page you host |
Because `saveProgress` and `loadProgress` are asynchronous, wait for the result before you use it.
Testing
Open your game on your own computer. The SDK saves to that browser, and `init` reports `onKoolGames: false`. If nothing saves, check the console for messages starting with "KoolGames:".