Skip to main content

Player manager

The manager rules over the player state and his synchronization with the server. After SDK initialization, there starts the process of player’s synchronization with the server. Upon completion, the ready event will be called.

Overview

Player manager properties FREE:

// The player is logged in
gp.player.isLoggedIn;
// The player uses one of the login methods (authorization, secret code)
gp.player.hasAnyCredentials;
// Player waiting promise
gp.player.ready;

The player is initialized automatically; you can wait for readiness:

await gp.player.ready;
// The player is ready

// The player is ready
gp.player.on('ready', () => {});

Synchronization

+0-1 Request

After making changes in the player, you can send them to the server and save locally on the device. It is recommended to synchronize the player after completing a level and receiving rewards, as well as immediately after important actions, such as purchasing an item.

// Sync, returns a promise
gp.player.sync();

// Overwrite the character on the server (local takes priority)
gp.player.sync({ override: true });

// Sync data with the selected storage
// "preferred" - specified storage in the control panel
// "cloud" - our cloud + platform's cloud + local storages
// "platform" - platform's cloud + local storages
// "local" - local storages
gp.player.sync({ storage: 'preferred' });

// Player synchronized (success === true)
gp.player.on('sync', (success) => {});
tip

Limitation on saved data: no more than 1 MB per player.

Synchronize with Selected Storage

You can save progress both in the GamePush cloud and locally and in the platform's cloud.

Storage types:

  • preferred +0-1 Request - Preferred storage, determined in the control panel.

  • cloud +1 Request - GamePush's cloud. At the time of saving we also replicate data in the platform's cloud and local storages for maximum reliability.

  • platform FREE - Platform's cloud.

    • 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
    • CoolMathGames
    • Android (alternative stores)
    • Web (custom site)
  • local FREE - Local storages, parallel saving in multiple types of local storages for reliability without losing 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
      • CoolMathGames
      • Android (alternative stores)
  • There are 2 options to implement saving in the desired storage:

    1. Set the storage in the control panel for the platform. In this case, when synchronizing normally, all data will be saved in the storage specified in the control panel.

    1. Specify the data storage during synchronization in the code:
    // By default, synchronize with the storage specified in the control panel
    gp.player.sync({ storage: 'preferred' });

    // Sync with GamePush cloud
    gp.player.sync({ storage: 'cloud' });

    // Sync with platform's cloud
    gp.player.sync({ storage: 'platform' });

    // Sync with local storage
    gp.player.sync({ storage: 'local' });

    Automatic Synchronization

    To simplify synchronization control, you can enable automatic synchronization in the required storages. Data will be saved only if player fields have been changed.

    // Enable synchronization
    gp.player.enableAutoSync({
    // synchronization frequency in seconds
    interval: 30,
    // By default, synchronize with the storage specified in the control panel
    storage: 'preferred',
    });

    // Disable synchronization
    gp.player.disableAutoSync({ storage: 'preferred' });

    // Examples:

    // Enable synchronization with local storage every 5 seconds
    gp.player.enableAutoSync({
    interval: 5,
    storage: 'local',
    });

    // Sync with GamePush cloud every 30 seconds
    gp.player.enableAutoSync({
    interval: 30,
    storage: 'cloud',
    });

    Additional Synchronization of Public Fields in the Cloud

    In cases where you are satisfied with local storage or the platform's cloud, but you have social mechanics, such as leaderboards, you can additionally check "When synchronizing, save public fields to the cloud" in the platform settings.

    This way, you can continue to save to the preferred storage and once you update a public field, it will be saved in our cloud on the next synchronization method call. Save to cloud storage will not occur until a public field is changed.

    Loading

    +1 Request

    You can load the player by force from the server (by overwriting the local one).

    // Load, returns the promise
    gp.player.load();

    // The player is loaded (success === true)
    gp.player.on('load', (success) => {});

    Login

    +0-1 Request

    Show the overlay with login options. This is currently the platform login (if it is supported) and login by secret code.

    // Login, returns the promise
    gp.player.login();

    // The player is logged in (success === true)
    gp.player.on('login', (success) => {});

    Logout

    +0-1 Request

    Logout player. If supported logout on platform.

    // Logout, returns the promise
    gp.player.logout();

    // The player is logged out (success === true)
    gp.player.on('logout', (success) => {});

    Fetching the fields

    +1 Request

    See below what player fields are.
    You can fetch the player fields using the following method:

    // Fetch fields list, returns the promise
    gp.player.fetchFields();

    // Fields are fetched (success === true)
    gp.player.on('fetchFields', (success) => {});

    Notification of other windows

    FREE

    If the player opens another window with the game at the same time as the other open windows of the game - all other windows will receive event:connect. You can subscribe to it like this:

    // will be triggered when the player opens another window with the game
    gp.on('event:connect', () => {});

    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!