Skip to content

Thrunium / Reference

Storage

Save data such as high scores and game progress on the player's device, in cookies. It also holds the Thrun db user-token flow for saving in Thrungames' cloud, which is a work in progress and does not work yet.

General

set(name, value)

  • name string

    Name to save the value under.

  • value unknown

    The value to save; anything but a string is converted with String().

returns true

Saves a value on the player's device, in a cookie that lasts 365 days and belongs to the current page's path. The value is turned into a string, so store objects with JSON.stringify and read them back with JSON.parse. Keep values small: browsers drop a cookie over 4 KB. Always returns true.

Since 1.1.0 values may contain any character: %, ;, line breaks and other control characters, and leading or trailing spaces are escaped, and get() turns them back. 1.0.0 cut values off at a ;.

JavaScript
THRUNIUM.Storage.set('highscore', 120);
THRUNIUM.Storage.get('highscore'); // '120'
JavaScript
var save = { level: 3, coins: 40 };
THRUNIUM.Storage.set('save', JSON.stringify(save));

get(name)

  • name string

    Name the value was saved under.

returns string | null

Reads a value saved with set(), as a string, or null if nothing is saved under name. Values saved by 1.0.0 still read back. Since 1.1.0 it no longer throws when another cookie on the page contains a malformed % escape.

JavaScript
var highscore = Number(THRUNIUM.Storage.get('highscore')) || 0;
JavaScript
var saved = THRUNIUM.Storage.get('save');
var save = saved ? JSON.parse(saved) : { level: 1, coins: 0 };

setReqToken(token)

  • token string

    The game's request token.

Sets the request token of the Thrun db user-token flow, a way to save data in Thrungames' cloud. The flow is a work in progress: it does not work in this version and will change when authentication is added.

reqUserToken(options)

  • options StorageOptions

    The callbacks to call with the result.

  • options.succes (e: unknown) => voidoptional

    Called when the player has granted access. Misspelled on purpose; kept for compatibility.

  • options.error (e: unknown) => voidoptional

    Called with { type, event } when the request fails or the player rejects it; type is 'ajaxFailed' or 'userRejected'.

Asks Thrungames for a user token for the request token set with setReqToken(), so the game can save data in Thrungames' cloud (Thrun db). The flow is a work in progress: it does not work in this version, so do not rely on its callbacks. It will change when authentication is added.