๐ Quick Start
Integrate the AI Games SDK into your game in 3 steps.
1
Add SDK Script
HTML
<script src="/sdk/v1/aigames-sdk.js"></script>
2
Initialize SDK
JS
AIGM.init();
AIGM.on('ready', function(features) {
console.log('SDK ready!', features);
AIGM.loading.start();
// ... load your game assets ...
AIGM.loading.stop();
AIGM.game.gameplayStart();
});3
Add Ads
JS
// Interstitial (between levels) await AIGM.ad.commercialBreak(); // Rewarded (optional bonus) const result = await AIGM.ad.rewardedBreak(); if (result.viewed) givePlayerReward();
๐ฎ Lifecycle API
| Method | Description |
|---|---|
AIGM.init(config?) | Initialize SDK. config: { debug, heartbeatInterval, gameId } |
AIGM.loading.start() | Notify asset loading started |
AIGM.loading.stop() | Asset loading complete (game ready) |
AIGM.game.gameplayStart() | Actual gameplay started |
AIGM.game.gameplayStop() | Gameplay paused (menu, level transition) |
AIGM.game.happytime() | Celebration effect (boss kill, highscore). 30s cooldown, 10/session |
๐บ Ads API
Supports both Promise and Callback patterns.
Promise
// Promise style (recommended)
const result = await AIGM.ad.requestAd('rewarded');
if (result.viewed) {
givePlayerReward();
}
// Convenience methods
await AIGM.ad.commercialBreak(); // interstitial
await AIGM.ad.rewardedBreak(); // rewardedCallback
// Callback style
AIGM.ad.requestAd('interstitial', {
adStarted: function() {
pauseGame();
muteAudio();
},
adFinished: function() {
resumeGame();
unmuteAudio();
},
adError: function(error) {
console.warn('Ad failed:', error);
resumeGame();
}
});โ ๏ธAlways pause your game and mute audio when an ad starts. Pre-roll ads (before game start) are prohibited.
๐ค User Module
JS
const user = AIGM.user.getUser();
// { isLoggedIn: true, displayName: "Player1", avatar: "https://...", id: "usr_xxx" }
// or { isLoggedIn: false }
// Request login
AIGM.user.showAuth();
// Listen for auth changes
AIGM.on('auth:changed', function(user) {
if (user.isLoggedIn) {
showWelcome(user.displayName);
}
});๐พ Data Storage
Save game progress to the server. Logged-in users get cloud sync, guests use localStorage.
JS
// Save progress
await AIGM.data.setItem('level', '5');
await AIGM.data.setItem('score', '12500');
// Load progress
const level = await AIGM.data.getItem('level');
const score = await AIGM.data.getItem('score');
// Delete
await AIGM.data.removeItem('level');
await AIGM.data.clear(); // remove all| Limit | Value |
|---|---|
| Value size (per key) | 64 KB |
| Keys (per game) | 100 |
| Total quota (user+game) | 1 MB |
๐งช Testing
Test mode auto-activates on localhost. Ads run as mocks and all events are logged to console.
JS
// Test mode is auto-detected on localhost.
// Or force it with URL param: ?aigm_debug=true
// Debug API (test mode only)
AIGM.debug.getLog(); // All events
AIGM.debug.simulateAd('rewarded'); // Simulate ad
AIGM.debug.setAdDelay(3000); // Mock ad delay (ms)
AIGM.debug.simulateEvent('pause'); // Simulate event
// Check SDK state
console.log(AIGM.getState());
// { ready, standalone, testMode, authenticated, sdkVersion }๐ Full Example
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My AI Game</title>
<script src="/sdk/v1/aigames-sdk.js"></script>
</head>
<body>
<canvas id="game" width="960" height="540"></canvas>
<script>
// 1. Initialize SDK
AIGM.init();
// 2. Wait for platform ready
AIGM.on('ready', function(features) {
console.log('Platform ready:', features);
// 3. Start loading assets
AIGM.loading.start();
loadGameAssets().then(function() {
AIGM.loading.stop();
startGame();
});
});
// 4. Handle platform events
AIGM.on('pause', function() { pauseGame(); });
AIGM.on('resume', function() { resumeGame(); });
AIGM.on('mute', function(muted) { setMute(muted); });
function startGame() {
AIGM.game.gameplayStart();
}
// 5. Show ads between levels
async function onLevelComplete(level) {
AIGM.game.gameplayStop();
// Save progress
await AIGM.data.setItem('lastLevel', String(level));
// Show interstitial
await AIGM.ad.commercialBreak();
// Boss defeated? Celebrate!
if (level % 5 === 0) AIGM.game.happytime();
AIGM.game.gameplayStart();
}
// 6. Rewarded ads for bonus
async function watchAdForBonus() {
AIGM.game.gameplayStop();
var result = await AIGM.ad.rewardedBreak();
if (result.viewed) {
givePlayerCoins(100);
}
AIGM.game.gameplayStart();
}
</script>
</body>
</html>๐ฏ Playground
Write code and test it live