Custom User Data (JS)¶
Save any small piece of data on a player as a key and a value, such as CURRENT_LEVEL = 5 or the player's chosen color. It is stored on GameFuse, so it follows the player to any device.
signIn and signUp download the player's data, so reading it makes no request. Keys and values are strings.
const player = GameFuseUser.CurrentUser;
// Read (downloaded by signIn and signUp). A key that is not set gives "".
console.log(player.getAttributes()); // { CURRENT_LEVEL: "4", ... }
console.log(player.getAttributeValue('CURRENT_LEVEL')); // "4"
console.log(player.getAttributesKeys()); // ["CURRENT_LEVEL", ...]
// Save one key. Keys and values are strings.
player.setAttribute('CURRENT_LEVEL', '5', function (message, hasError) {
if (hasError) {
console.error('Could not save: ' + message);
return;
}
console.log(player.getAttributeValue('CURRENT_LEVEL')); // "5"
});
// Save a list or object by turning it into a string first, and back when you read it.
player.setAttribute('UNLOCKED_WORLDS', JSON.stringify([1, 2, 5]), function (message, hasError) {
if (!hasError) {
const worlds = JSON.parse(player.getAttributeValue('UNLOCKED_WORLDS'));
console.log(worlds); // [1, 2, 5]
}
});
Save several keys at once¶
setAttributes saves a whole object in one request.
player.setAttributes({ CURRENT_LEVEL: '6', COLOR: 'green' }, function (message, hasError) {
if (hasError) {
console.error('Could not save: ' + message);
}
});
Change now, save later¶
setAttributeLocal changes a value in the browser only. syncLocalAttributes then sends every change made that way in one request. Use this for values that change often, and save at a checkpoint.
player.setAttributeLocal('COINS_THIS_RUN', '12');
player.setAttributeLocal('COINS_THIS_RUN', '13');
console.log(player.getDirtyAttributes()); // { COINS_THIS_RUN: "13" }, not saved yet
player.syncLocalAttributes(function (message, hasError) {
if (!hasError) {
console.log(player.getDirtyAttributes()); // {}
}
});
Delete a key¶
player.removeAttribute('COLOR', function (message, hasError) {
if (!hasError) {
console.log(player.getAttributeValue('COLOR')); // ""
}
});
Reference¶
| Call | What it does |
|---|---|
getAttributes() |
All of the player's data, as an object |
getAttributesKeys() |
The keys, as an array |
getAttributeValue(key) |
One value, or "" if the key is not set |
setAttribute(key, value, callback) |
Saves one key on GameFuse and updates getAttributes() |
setAttributes(keysAndValues, callback) |
Saves several keys in one request |
setAttributeLocal(key, value) |
Changes a value in the browser only |
getDirtyAttributes() |
The changes made with setAttributeLocal that are not saved yet |
syncLocalAttributes(callback) |
Saves those changes |
removeAttribute(key, callback) |
Deletes a key on GameFuse |
All of these are on GameFuseUser.CurrentUser.