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. List 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));
});
or in async
context:
let keys = await wakool.userStorage.keys();
keys.forEach(key => console.log(key));
10.3. Get item
Retrieve the data associated with the specified key:
wakool.userStorage.getItem(key);
Parameters:
name | type | description |
---|---|---|
key | string | the key associated with the data |
Example:
wakool.userStorage.getItem('high-score').then(result => {
if (result) {
console.log('High score:' + result);
}
});
or in async
context:
let highScore = await wakool.userStorage.getItem('high-score');
console.log('High score:' + highScore);
10.4. Set item
Insert a data with a unique key, overwrite existing data:
wakool.userStorage.setItem(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.setItem('high-score', 30125);
// or an object
wakool.userStorage.setItem('settings', {
fullscreen: true,
volume: 30,
});
10.5. Remove item
Delete the data previously associated with the specified key.
wakool.userStorage.removeItem(key);
Parameters:
name | type | description |
---|---|---|
key | string | the key associated with the data |
Example:
wakool.userStorage.removeItem('high-score');
10.6. Game global storage
The only differences between the game global storage and the user storage is that the game global 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.