Data Storage
Use Cases
Data Storage allows you to store data in a database server to be loaded again at any time. Some use cases include:
- Storing player progress and data
- Leaderboards and highscores
- Saving map state and data
- Adding item unlocks and rewards
- Sharing data between games and collaborating with other devs
Only use Data Storage when you want to save persistent data. If you want to save data locally, use Cookies instead.
Storing Data
Save data on the database using KrunkScript (server-side only):
# set values on specific player (1 every 10s rate limit):
GAME.STORAGE.set(player.accountName, {
apples: 10, # set apples to 10
title: "knight" # set string value
});
# update values on specific player (plus or minus values):
GAME.STORAGE.update(player.accountName, {
apples: -4, # decrease apples by 4
oranges: 2 # add 2 oranges
});
# transact values on specific player:
# a transaction requires that no values fall below 0
public action callback(obj data, bool success) {
if (success) {
# transaction successfully completed
# update to latest synced values
player.gold = data.gold;
player.apples = data.apples;
} else {
# transaction failed
}
};
GAME.STORAGE.transact(player.accountName, {
gold: -5, # lose 5 gold
apples: 10 # add 10 apples
}, false, callback);
The access parameter determines how the data can be accessed:
- private (false, default): data can only be accessed on games by the same owner (read/write)
- global (true): data can be accessed by anyone (read only)
Loading Data
Load data from the database using KrunkScript (server-side only):
# get all data for a specific player:
public action dataReady(obj data, bool success) {
if (data && success) {
GAME.log(data.coins); # access data
}
};
GAME.STORAGE.load(player.accountName, "", dataReady);
# load data for specific game:
public action dataReady(obj data, bool success) {
if (data && success) {
GAME.log(data.wins); # access data
}
};
GAME.STORAGE.load(player.accountName, "lava_run", dataReady);
# NOTE: you can only load data from other games that
# you published or have saved specific data as: global
Storing global data will allow other game devs to access that data as well. You could use this to allow players to unlock unique items if they complete certain challenges in another game and vice versa.
Highscore Example
A common pattern for saving highscores:
# newScore comes from game
if (newScore > player.highscore) {
public action callback(obj data, bool success) {
if (success) {
player.highscore = data.score;
}
}
GAME.STORAGE.set(player.accountName, {
score: newScore,
}, false, callback);
}
Cookies & Local Storage
If you want to save certain data locally on the client, use KrunkScript's built-in COOKIE feature (client-side only):
# save local data
GAME.COOKIES.save(
"test", # str key value
100 # str data value
);
# load local data
str value = GAME.COOKIES.load(
"test" # str key value
);
# delete local data
GAME.COOKIES.remove(
"test" # str key value
);
# check if value exists
GAME.COOKIES.has(
"test" # str key value
);
Limits & Constraints
| Constraint | Value |
|---|---|
| set / update / transact rate | Once per 10 seconds per player |
| load calls per connection | 5 per player |
| Unique keys per game | 30 |
| Key length | 20 characters max |
You can only save player data for a player that is currently in the game. Each property in the object you store is counted as a unique key in the database.