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

1ysdk.player.get_info(
2    callback: function,
3    options: {
4        scopes: boolean|nil,
5        signed: boolean|nil
6    }
7)

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:
 1player = {
 2    logged_in: boolean,
 3    unique_id: string,
 4    name: string,
 5    photo: {
 6        small: string,
 7        medium: string,
 8        large: string
 9    }
10}

Example

 1function init_player()
 2  ysdk.player.get_info(function(self, player)
 3      if player then
 4        display_player(self, player)
 5      else
 6        ysdk.player.open_auth_dialog(function(self, authorized)
 7          ysdk.player.get_info(display_player, {})
 8        end)
 9      end
10    end,
11    {})
12end
13
14function display_player(player)
15  if player then
16    print(player.name, signature)
17  end
18end

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

1function log_ids()
2  ysdk.player.get_ids_per_game(function(self, ids)
3    for _, value in ipairs(ids) do
4      print(value.app_id, value.user_id)
5    end
6  end)
7end

Repository