10. Storage

For pure client games without their own servers, but still need to store user settings or game progress across devices and browsers, the SDK provides a storage for developers to save/load these data.

※Be aware that the storage functions aren't secure. If security is a concern please use your own backend server to store data.

※ This function is available since SDK version 3.0.4

10.1. User storage

The user storage is a space per game per player and can be used to store player settings or game progress, etc.

10.1.1. Description

Storage provides four functions:

All functions of the storage are executed asynchronously and return a Promise object.

10.1.2. Obtain the user storage:

let storage = wakool.userStorage;

10.2. KEYS

Obtain the existing keys in the storage:

wakool.userStorage.keys(path = null);

Parameters:

name type description
path string Not required, filter the results under certain directory

※ The results can be filtered if the keys are structured as a path using / when put into the storage.

Example:

wakool.userStorage.keys().then(keys => {
    keys.forEach(key => console.log(key));
});

10.3. GET

Retrieve the data associated with the specified key:

wakool.userStorage.get(key);

Parameters:

name type description
key string the key associated with the data

Example:

wakool.userStorage.get('high-score').then(result => {
    if (result) {
        console.log('High score:' + result);
    }
});

10.4. PUT

Insert a data with a unique key, overwrite existing data:

wakool.userStorage.put(key, value);

Parameters:

name type description
key string a unique key
value any the data to be put into the storage

※ The key can be structured like a path using /

Example:

// the data can be a number or a string
wakool.userStorage.put('high-score', 30125);

// or an object
wakool.userStorage.put('settings', {
    fullscreen: true,
    volume: 30,
});

10.5. REMOVE

Delete the data previously associated with the specified key.

wakool.userStorage.remove(key);

Parameters:

name type description
key string the key associated with the data

Example:

wakool.userStorage.remove('high-score');

10.6. Game storage

The only differences between the game storage and the user storage is that the game storage is shared by all players of the game.

10.6.1. Obtain the game storage:

let storage = wakool.appStorage;

otherwise the API of the game storage is exactly the same with the user storage.

results matching ""

    No results matching ""