Skip to content

Data

The data module allows to save and retrieve user data for logged in CrazyGames users. The data will also be synced on all the devices where the user plays the game.

If the user is not logged in, the data module will store the game data in LocalStorage. If the user logs in later, the LocalStorage game data will be synced and backed up on the user's account.

Warning

If you intend to use the data module, don't forget to select the appropriate toggle in the "Does your game save progress?" form when submitting the game. The data module will be disabled otherwise.

Using the data module

Initialization

Before using any methods from the data module, please be sure the SDK is initialized.

await window.CrazyGames.SDK.init();

We recommend that you do this during the loading screen of your game since the SDK preloads all the game data when it is initialized. This may take some time, depending on how much user data is stored.

Usage

The data module has the same API as the localStorage:

clear(): void;
getItem(key: string): string | null;
removeItem(key: string): void;
setItem(key: string, value: string): void;

You can call methods from the data module like this:

window.CrazyGames.SDK.data.setItem("gold", 100);

The data module has the same API as the PlayerPrefs:

void SetInt(string key, int value);
int GetInt(string key);
int GetInt(string key, int defaultValue);
void SetFloat(string key, float value);
float GetFloat(string key);
float GetFloat(string key, float defaultValue);
void SetString(string key, string value);
string GetString(string key);
string GetString(string key, string defaultValue);
bool HasKey(string key);
void DeleteKey(string key);
void DeleteAll();

You can call methods from the data module like this:

CrazySDK.Data.SetInt("gold", 100);

Initialization

Before using any methods from the data module, please be sure the SDK is initialized.

await window.ConstructCrazySDK.init();

Our demo project contains a loading layout that loads the SDK, initializes it and then loads the game.

Using the data module

The data module has the same API as the localStorage:

clear(): void;
getItem(key: string): string | null;
removeItem(key: string): void;
setItem(key: string, value: string): void;

The getItem() method retrieves stored data for a specific key. Use it to fetch previously saved user data:

const value = window.ConstructCrazySDK.data.getItem("keyName");

The setItem() method stores data for a specific key. The data will be synced across the user's devices. In this code, first we check if the data we want to set already exists in our storage or not:

var key = document.getElementById("keyVal").value;
var res = window.ConstructCrazySDK.data.getItem(key);
var val = document.getElementById("mainVal").value;
if (res != null) {
var final = parseInt(res) + parseInt(val);
window.ConstructCrazySDK.data.setItem(key, final);
} else {
window.ConstructCrazySDK.data.setItem(key, val);
}

Avoid losing user progress

In general, it's a good practice to always getData before setting data to ensure that the player's previous progress isn't lost.

Guest user behaviour

For guest users, the data module stores the game data in localStorage. When a guest user signs in, you don't need to do anything. Our SDK will automatically load the account game data if there is any, or if this user hasn't played your game before, the SDK will transfer the guest data to the user account.

When the user signs out, the SDK will revert back to using the guest game data.

QA Tool

The data module works in QA Tool. You can delete your progress (using the cloud icon) and logout to simulate different users on the same device.

Data saving

The SDK debounces data saving with 1 second, meaning that multiple calls to the methods will be saved after 1 second. There may be exceptions in various cases, when data saving may be debounced with more time, up to 30 seconds.

There is a 1MB data limit. If you are approaching it, you will see warnings in the browser console. The data won't be backed up anymore if it exceeds 1MB.

Integrating data module into already published games

Since the data module offers the same API as window.localStorage, it is quite easy to integrate it into your already published games. To avoid players losing their data, you should copy all the existing localStorage keys into the data module for the players who played your game before.

Since the Data module offers the same API as PlayerPrefs, it is quite easy to integrate it into your already published games. To avoid players losing their data, you should copy all the existing PlayerPrefs keys into the Data module for the players who played your game before.

Since the data module offers the same API as window.localStorage, it is quite easy to integrate it into your already published games. To avoid players losing their data, you should copy all the existing localStorage keys into the data module for the players who played your game before.