Function reference
Everything the HGSDK object offers. The SDK creates it as soon as it loads, before your game's code runs.
Lifecycle of a game
- If your game has a pause menu or goes back to a menu:
gameplayStop()when gameplay stops andgameplayStart()when it continues. - If your game has a score, send it whenever you want with
sendScore(score): when losing, completing a level… It doesn't stop gameplay.
Gameplay
| Function | What it's for | |
|---|---|---|
HGSDK.gameplayStart() | Gameplay starts or continues. From here on, gestures belong to the game and play time counts. | Required |
HGSDK.gameplayStop() | Gameplay stops: the player loses, pauses or goes to the menu. Play time stops counting and the player can swipe to switch games. | Required |
HGSDK.sendScore(score) | Sends a score (whole number from 0 to 10,000,000; decimals are rounded down). If the player is signed in, it's saved as their high score when it beats the previous one and appears in the rankings. It doesn't stop gameplay. Only if your game has a score. | Optional |
Each player has a single score in each ranking (today, this week and all time): their best. Sending many scores only keeps the highest one.
Ads
Your game never loads ads by itself: it asks the website with HGSDK.showAd({ type }) and the website shows the ad on top of the game, frozen and muted meanwhile. reward returns a promise with { rewarded }. If you don't use promises, pass a function as second argument: it's called when the ad ends and receives that same { rewarded }. break returns nothing: your game continues on its own when the ad ends.
type | When | Returns |
|---|---|---|
'break' | Between runs, for example before playing again. | Nothing. Your game continues on its own when the ad ends. |
'reward' | The player chooses to watch it in exchange for something in the game (revive, continue…). | rewarded: true only if they watched it to the end. Give the prize only then. |
// one revive per run, only if the player asks for it
// option 1: with a promise
async function offerRevive() {
const { rewarded } = await HGSDK.showAd({ type: 'reward', name: 'revive' });
if (rewarded) revive(); else gameOver();
}
// option 2: with a function as second argument
function offerRevive() {
HGSDK.showAd({ type: 'reward', name: 'revive' }, function (r) {
if (r.rewarded) revive(); else gameOver();
});
}
// before playing again, never before the first run
if (runs > 0) HGSDK.showAd({ type: 'break' });
runs++;
startRun();
- Your game must work without ads.
rewardedcan befalseat any moment: no ad available, ads switched off or the player closed it early. Never leave the player waiting. - Don't call
gameplayStop()orgameplayStart()because of an ad: the SDK freezes and resumes your game on its own. Call them only when the run really stops (losing, pause, menu) and when it continues. If your game does it, it's rejected until you fix it. - The website decides how often. It shares a single counter between the feed and all the games, so breaks never come one after another. Ask whenever it makes sense in your game: if it's too soon, simply no ad is shown. This only applies to
break: arewardis shown whenever the player asks for it. - A reward never gives score or anything that ends up in the rankings directly. Revives, continues, extra lives or skins are fine.
- Ask for
breakads at natural moments — losing or finishing a level — never in the middle of the action, and not in the player's first run (the website doesn't know which run it is: that part is up to your game). nameis optional: a short label (up to 40 characters). In JavaScript you don't need it, each call gets its own answer; in GameMaker it comes back in the result to tell your rewards apart.- One ad at a time. If you ask for a
rewardwhile another ad is on screen, it answersrewarded: falsestraight away.
Several rewards
Your game can have as many rewards as it needs (revive, double the coins, a skin…). Give each one its own name: a short label (up to 40 characters) that says which reward it is. In JavaScript, each showAd call receives its own answer, so each reward handles its own result:
// when they ask to revive
async function offerRevive() {
const { rewarded } = await HGSDK.showAd({ type: 'reward', name: 'revive' });
if (rewarded) revive(); else gameOver();
}
// at another moment: to double their coins
async function offerDoubleCoins() {
const { rewarded } = await HGSDK.showAd({ type: 'reward', name: 'double_coins' });
if (rewarded) coins *= 2;
}
Version
HGSDK.version is the version of the SDK that's loaded, for example "1.8.7" (read only). You don't need it to integrate your game.
HypercasualGaming