HypercasualGaming SDK
Guide

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

Cover→gameplayStart()→playing→gameplayStop()→menu or end screen

Gameplay

FunctionWhat 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, force)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. force is optional and false by default: with true the score replaces the saved one even when it is lower, for a score that goes up and down (like the coins a player holds), so the rankings show its current value. Scores sent less than 2 seconds after the previous one are ignored. It doesn't stop gameplay. Only if your game has a score.Optional
HGSDK.getUser()Tells who is playing. It returns a promise with { signedIn, name }: the account name, or signedIn: false for a guest (or when the game isn't on the website). Useful to ask players to sign in before a feature that needs an account, like the data system.Optional
HGSDK.getLanguage()Tells the language the player chose on the website. It returns a promise with a two-letter code, like "en" or "es". Outside the website it gives the browser's language. Useful to start a translated game in the right language; it's still a good idea to let players change it in the game.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.

typeWhenReturns
'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();

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;
}

Data system

HGSDK.data lets your game save and read entries that all its players share: a market, levels made by players, a ranking… It has its own page, with the warnings, the functions, the errors, the rules and the control panel: Data system.

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.