Player data

You can save the player's game state data (such as completed levels, experience, or in-game purchases) on the Yandex server or send this data to your server. You can also provide a more personalized experience by using data from the user's Yandex profile — for example, their name.

Player Information

ysdk.player.get_info(
    callback: function,
    options: {
        scopes: boolean|nil,
        signed: boolean|nil
    }
)

callback: function — callback of the called method. Has the following form:

function(self, player: table|nil, signature: string|nil): nil
  • player: table — player information. Contains the following properties:
player = {
    logged_in: boolean,
    unique_id: string,
    name: string,
    photo: {
        small: string,
        medium: string,
        large: string
    }
}

Example

function init_player()
  ysdk.player.get_info(function(self, player)
      if player then
        display_player(self, player)
      else
        ysdk.player.open_auth_dialog(function(self, authorized)
          ysdk.player.get_info(display_player, {})
        end)
      end
    end,
    {})
end

function display_player(player)
  if player then
    print(player.name, signature)
  end
end

User Authorization

If the player isn't logged in, you can use the ysdk.player.open_auth_dialog() method to call the authorization window.

ysdk.player.open_auth_dialog(callback: function)

callback: function — callback of the called method. Has the following form:

function(self, authorized: boolean): nil
  • authorized: boolean — whether the player is authorized or not.

In-game data

To work with the user's in-game data, use the Player object methods:

  • ysdk.player.set_data(data, flush) — Saves the user data. The maximum data size must not exceed 200 KB.

    • data: table — An object containing key-value pairs.
    • flush: boolean — Determines the order for sending data. If the value is "true", the data is immediately sent to the server. If it's "false" (default), the request to send data is queued.

The method returns a Promise that indicates whether the data was saved or not.

At flush: false, the returned result only shows the data validity (the data has been queued and will be sent later). At the same time, the ysdk.player.get_data() method will return the data set by the last ysdk.player.set_data()call, even if it has not been sent yet.

  • ysdk.player.get_data(callback, keys) — requests the user's in-game data stored in the Yandex database.

    • callback: fun(self, data: table<string, any>) — handler function for processing the received in-game user data.
    • keys: table<number, string> — the list of keys to return. If the keys parameter is missing, the method returns all in-game user data.
  • ysdk.player.set_stats(stats) — saves the user's numeric data. The maximum data size must not exceed 10 KB.

    • stats: table<string, number> — object containing key-value pairs, where each value must be a number.

Note

For frequently changing numeric values (points, experience, in-game currency) use this method instead of ysdk.player.set_data().

  • ysdk.player.increment_stats(increments) — changes in-game user data. The maximum data size must not exceed 10 KB.
    • increments: table<string, number> — an object containing key-value pairs, where each value must be a number.
  • ysdk.player.get_stats(callback, keys) — retrieve the user's numerical data stored in the Yandex database.
  • callback: fun(self, data: table<string, number>|nil) — handler function for working with the received numerical user data.
  • keys: string[] — the list of keys to return. If the keys parameter is missing, the method returns all in-game user data.

User identifiers

ysdk.player.get_ids_per_game(callback: function)

callback: function — callback of the invoked method. Has the following form:

function(self, ids: {appID: number, userID: string}[]): nil
  • ids — user identifiers in all games of the developer, for which the user has explicitly consented to the transfer of personal data. Contains the following properties:
    • app_id: string — application ID.
    • user_id: string — user ID.

Example

function log_ids()
  ysdk.player.get_ids_per_game(function(self, ids)
    for _, value in ipairs(ids) do
      print(value.app_id, value.user_id)
    end
  end)
end

Repository