8. Storage
For pure client games without their own servers, typically developers store the game progress or player settings in localStorage. The SDK provides a cloud storage space as an alternative to localStorage. This storage is linked to the player's ID, making it less likely to be lost and accessible across devices and browsers.
※Be aware that the storage functions aren't secure. If security is a concern please use your own backend server to store data.
8.1. User storage
User storage is allocated per game per player and can be used to store player settings, game progress, and more.
8.1.1. Description
Storage provides four functions:
All functions of the storage are executed asynchronously and return a Promise object.
8.1.2. Obtain the user storage:
let storage = wakool.userStorage;
8.2. List keys
Retrieve the existing keys in the storage:
wakool.userStorage.keys(path = null);
8.2.1. 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 set into the storage.
8.2.2. Example:
let keys = await wakool.userStorage.keys();
keys.forEach(key => console.log(key));
8.3. Get item
Retrieve data associated with the specified key:
wakool.userStorage.getItem(key);
8.3.1. Parameters:
name | type | description |
---|---|---|
key | string | the key associated with the data |
8.3.2. Example:
let highScore = await wakool.userStorage.getItem('high-score');
console.log('High score:' + highScore);
8.4. Set item
Insert data with a unique key, overwriting any existing data:
wakool.userStorage.setItem(key, value);
8.4.1. 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 /
8.4.2. Example:
// the data can be a number or a string
wakool.userStorage.setItem('high-score', 30125);
// or an object
wakool.userStorage.setItem('settings', {
fullscreen: true,
volume: 30,
});
8.5. Remove item
Delete the data associated with the specified key.
wakool.userStorage.removeItem(key);
8.5.1. Parameters:
name | type | description |
---|---|---|
key | string | the key associated with the data |
8.5.2. Example:
wakool.userStorage.removeItem('high-score');