The engine provides built-in functions to save and load player progress. These functions store the user's game data persistently on the engine server, allowing the game state to be restored after restarting or continued from any device.
| Function | Description |
|---|---|
SavePlayerData(data) |
Saves the current game state.
Receives a JavaScript object containing the data you want to store.
Example: SavePlayerData({ score: 120, level: 2, lives: 3 });
|
LoadPlayerData() |
Loads previously saved data.
Returns an object or a "empty" status if no saved game exists.
|
This example shows how to load saved data when the game starts
and how to save player progress using async / await.
var Score = 0;
async function Start(obj) {
let data = await LoadPlayerData();
if (data.status !== "empty") {
Score = data.score || 0;
console.log("Game loaded:", data);
}
}
async function Save_Game(obj) {
await SavePlayerData({
score: Score,
level: 3,
items: ["sword", "key"]
});
console.log("Game saved");
}
function Update(obj, dt) {
console.log("Current score:", Score);
}
Since engine scripts are isolated, functions must be invoked using
CallFunction() or sendMessage().
// Call Save_Game on the same object
CallFunction(obj, "Save_Game");
// Or from another script:
CallFunction("Player", "Save_Game");
You can also use a UI button and assign the action in its onClickUp() event:
async function Start(obj) {
onClickUp(obj, async () => {
await CallFunction("Player", "Save_Game");
});
}
The content of the save object is completely flexible and depends on the needs of each game. For example:
{
"score": 1500,
"level": 4,
"lives": 2,
"items": ["sword", "shield", "gem"]
}
LoadPlayerData() only once at game start (inside Start()).SavePlayerData() every frame.async / await to keep your code clean and readable.