Player data

You can:

  • Save game data (completed levels, experience, in-app purchases, etc.) on the Yandex server using SDK methods or transfer it to your own server. Cloud saves allow users to continue playing on different devices.
  • Personalize the game using data from the user's Yandex profile, such as 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 true is set, the data will be sent to the server immediately; false (default value) — the data sending request will be 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) — a handler function for working with the received numeric user data.
    • keys: string[] — a 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