Storage
Overview
The module allows working with local and cloud storage of platforms.
Types of storage:
- platform FREE - Platform's cloud storage + local storages (maximum replication).
- ✅Yandex Games
- ✅CrazyGames
Platforms without support
- ❌GamePix
- ❌GameDistribution
- ❌GameMonetize
- ❌OK Games
- ❌SmartMarket
- ❌VK Games
- ❌VK Play
- ❌WG Playground
- ❌Kongregate
- ❌PlayDeck
- ❌Google Play
- ❌Telegram
- ❌beeline
- ❌Fotostrana
- ❌Y8
- ❌Android (alternative stores)
- ❌Web (custom site)
- local FREE - Local storages, simultaneous saving in multiple types of local storages for reliability without loss of progress on iOS.
- LocalStorage - standard local storage.
- IndexedDB - browser database.
- SafeStorage - safe storage provided by the platform.
- ✅Yandex Games
- ✅GamePix
- ✅CrazyGames
- ✅Web (custom site)
Platforms without support
- ❌GameDistribution
- ❌GameMonetize
- ❌OK Games
- ❌SmartMarket
- ❌VK Games
- ❌VK Play
- ❌WG Playground
- ❌Kongregate
- ❌PlayDeck
- ❌Google Play
- ❌Telegram
- ❌beeline
- ❌Fotostrana
- ❌Y8
- ❌Android (alternative stores)
You can switch between storage types at any time. Default value - platform.
- JavaScript
- Unity
// Set storage type - platform's cloud storage + local storages
gp.storage.setStorage('platform');
// Set storage type - local storages
gp.storage.setStorage('local');
// Set storage type - platform's cloud storage + local storages
GP_Storage.SetStorage(SaveStorageType.platform);
// Set storage type - local storages
GP_Storage.SetStorage(SaveStorageType.local);
Contextual Storage
FREEBy default, all data is stored in the player's context. This allows storing different data for different players and platforms.
Context consists of:
- Project ID
- Platform Type
- Test / Live site version
- Player ID on the platform
This way, we can separate data for different platforms, even using the same domain. And separate this data among players. You can store guest data separately from authorized player data.
With the get
and set
methods, you default to accessing data in the player's context. You can store any data types, we automatically convert them to the required format if the storage requires it.
The method is asynchronous. Not all storages can set data instantly. Some, for example, cloud or IndexedDB, do it with a delay, so we return a promise or send an event when the data is set.
Set Value
- JavaScript
- Unity
// Set value
await gp.storage.set('key', 'value');
// Set number
await gp.storage.set('gold', 1000);
// Set boolean value
await gp.storage.set('vip', true);
// Set any data type
await gp.storage.set('progress', {
class: 'warrior',
level: 5,
gold: 1000,
missions: [1, 5, 8],
});
// Set value
GP_Storage.Set("key", "value");
// Set number
GP_Storage.Set("gold", 1000);
// Set boolean value
GP_Storage.Set("vip", true);
Events on setting value
- JavaScript
- Unity
// Value set
gp.storage.on('set', ({ key, value }) => {});
//Subscribe to event
private void OnEnable()
{
GP_Storage.OnSetValue += OnSetValue;
}
//Unsubscribe to event
private void OnDisable()
{
GP_Storage.OnSetValue -= OnSetValue;
}
// Value set
private void OnSetValue(StorageField storage) => Debug.Log($"Set value: Key: {storage.key}, Value: {storage.value}");
Get Value
Getting values is also asynchronous.
If there is no data, the storage will return null.
- JavaScript
- Unity
// Get value
const value = await gp.storage.get('key');
// Get number
const gold = await gp.storage.get('gold');
// Get boolean value
const vip = await gp.storage.get('vip');
// Get any data type
const progress = await gp.storage.get('progress');
// progress.class
// progress.missions[0]
// Get value
GP_Storage.Get("key", OnGetValue);
// Get number
GP_Storage.Get("gold", OnGetValue);
// Get boolean value
GP_Storage.Get("vip", OnGetValue);
//Callback with value
private void OnGetValue(object value) => Debug.Log($"Get value: {value}");
Events on getting value
- JavaScript
- Unity
// Value retrieved
gp.storage.on('get', ({ key, value }) => {});
//Subscribe to event
private void OnEnable()
{
GP_Storage.OnGetValue += OnGetValue;
}
//Unsubscribe to event
private void OnDisable()
{
GP_Storage.OnGetValue -= OnGetValue;
}
// Value retrieved
private void OnGetValue(StorageField storage) => Debug.Log($"Get value: Key: {storage.key}, Value: {storage.value}");
Global Storage
FREEYou can set data regardless of the player's context, in the global scope. This way, you can set a value for all profiles or transfer data between profiles.
Set Value
- JavaScript
- Unity
// Set value
await gp.storage.setGlobal('key', 'value');
// Set number
await gp.storage.setGlobal('gold', 1000);
// Set boolean value
await gp.storage.setGlobal('vip', true);
// Set any data type
await gp.storage.setGlobal('progress', {
class: 'warrior',
level: 5,
gold: 1000,
missions: [1, 5, 8],
});
// Set value
GP_Storage.SetGlobal("key", "value");
// Set number
GP_Storage.SetGlobal("gold", 1000);
// Set boolean value
GP_Storage.SetGlobal("vip", true);
Events on setting value
- JavaScript
- Unity
// Value set
gp.storage.on('set:global', ({ key, value }) => {});
//Subscribe to event
private void OnEnable()
{
GP_Storage.OnSetGlobalValue += OnSetGlobalValue;
}
//Unsubscribe to event
private void OnDisable()
{
GP_Storage.OnSetGlobalValue -= OnSetGlobalValue;
}
// Value set
private void OnSetGlobalValue(StorageField storage) => Debug.Log($"Set Global value: Key: {storage.key}, Value: {storage.value}");
Get Value
Getting values is also asynchronous.
If there is no data, the storage will return null.
- JavaScript
- Unity
// Get value
const value = await gp.storage.getGlobal('key');
// Get number
const gold = await gp.storage.getGlobal('gold');
// Get boolean value
const vip = await gp.storage.getGlobal('vip');
// Get any data type
const progress = await gp.storage.getGlobal('progress');
// progress.class
// progress.missions[0]
// Get value
GP_Storage.GetGlobal("key", OnGetGlobalValue);
// Get number
GP_Storage.GetGlobal("gold", OnGetGlobalValue);
// Get boolean value
GP_Storage.GetGlobal("vip", OnGetGlobalValue);
//Callback with value
private void OnGetGlobalValue(object value) => Debug.Log($"Get Global value: {value}");
Events on getting value
- JavaScript
- Unity
// Value retrieved
gp.storage.on('get:global', ({ key, value }) => {});
//Subscribe to event
private void OnEnable()
{
GP_Storage.OnGetGlobalValue += OnGetGlobalValue;
}
//Unsubscribe to event
private void OnDisable()
{
GP_Storage.OnGetGlobalValue -= OnGetGlobalValue;
}
// Value retrieved
private void OnGetGlobalValue(StorageField storage) => Debug.Log($"Get Global value: Key: {storage.key}, Value: {storage.value}");
Stay in Touch
Other documents of this chapter available Here. To get started, welcome to the Tutorials chapter.
GamePush Community Telegram
: @gs_community.
For your suggestions e-mail
: [email protected]
We Wish you Success!