Skip to main content

Adding a Plugin to a Defold Project


You can use it in your own project by adding the github project as a Defold Library Dependency.


In the game.project file, you need to add the gamepush section and specify the game id and token. Other parameters are optional:

id = game identifier
token = token
description = There are not enough words to describe the awesomeness of the game
image = /img/ogimage.png

Additional parameters: (doc)

First, you need to initialize the SDK using the init method:

local gamepush = require("gamepush.gamepush")

if success then
-- initialization was successful
-- initialization error

To subscribe to an event, you need to define the corresponding method in the callbacks table:

local gamepush = require("gamepush.gamepush")

local function ads_start()
-- turn off the sound

local function ads_close(success)
-- turn on the sound

local function ads_reward()
-- give the player some gold

-- event functions can be assigned before SDK initialization = ads_start = ads_close = ads_reward
-- SDK initialization
if success then
-- show the preloader
-- ad is closed, we can do something
-- we do something else

-- show rewarded video ad

-- show rewarded video ad using a callback
if result then
-- give the player some crystals
-- initialization error

List of All Methods

You can find a detailed list of all methods here.

Calling Native Platform Methods

The function call_native_sdk(method, parameters, callback) is intended to call a native method, get an object, or access a field.

  • method: A string specifying the path to the method, object, or field separated by dots. If the path leads to an object or object field, then parameters and callback will be ignored.
  • parameters: The parameter for the called method (string, number, boolean, table). If multiple parameters are needed, they should be placed in an array (table). The order of parameters is determined by the array index. Passing functions as parameters is not supported!
  • callback: A callback function, it should be specified if the native method returns a promise. If callback == nil, the function returns the result; otherwise, it returns nil.

The result returned by the function follows these rules:

  1. If the method parameter refers to an object or an object's field:
  • If the result is a string, number, boolean, or object, the obtained data is returned.
  • If an exception occurs, then error data is returned as a table {error = "error description"}.
  1. If the method parameter refers to a function:
  • If the result is a string, number, boolean, or object, the obtained data is returned.
  • If an exception occurs, or the promise completes with an error, then error data is returned as a table {error = "error description"}.

callback(result): result - the outcome of the promise. If the promise ends with an error, then result = {error = "error description"}.

Advanced Actions with Promises

Sometimes, a promise returns an object with functions that may need to be executed later. For these situations, there is a mechanism for saving the object in JS and using it in subsequent API calls.

In these cases, the method parameter format for the call_native_sdk function can take forms such as:

  • var=path1.path2.path3: The object at path1.path2.path3 will be saved in the variable var.
  • var:method: Call a method from a previously saved object.
  • var2=var:method2: Invoke a method (the method should be a promise) from a previously saved object and save the result in the variable var2.

Examples of Various Calls

SDK RequestTypeFunction invocation and result
{error = 'Field or function "env" not found!'}
feedback.canReview()functioncall_native_sdk("feedback.canReview", nil, callback)
Callback will be called upon promise completion.
getLeaderboards().then(lb => {})functioncall_native_sdk("lb=getLeaderboards", nil, callback)
Callback will be called upon promise completion.
The result will be saved in a JS variable.
Callback will be called upon promise completion.
This call will access a previously saved JS variable; if it's not found, the function will return {error = "The 'lb' object has not been previously saved!"}

Example of Native Interaction with Yandex Platform:

local gamepush = require("gamepush.gamepush")

if result then
-- Get the Yandex environment variables, equivalent to ysdk.environment
local environment = gamepush.platform.call_native_sdk("environment")

-- Get the language of the Yandex.Games interface in ISO 639-1 format, equivalent to ysdk.environment.i18n.lang
local language = gamepush.platform.call_native_sdk("environment.i18n.lang")

-- Get leaderboards, equivalent to ysdk.getLeaderboards()
-- the promise returns an object, let's save it in the variable lb
gamepush.platform.call_native_sdk("lb=getLeaderboards", nil, function(leaderboards)
-- Record a new high score, equivalent to lb.setLeaderboardScore('leaderboard2021', 120);
-- we will refer to the variable lb
gamepush.platform.call_native_sdk("lb:setLeaderboardScore", { "leaderboard2021", 120 })
-- Get leaderboard data, equivalent to lb.getLeaderboardEntries('leaderboard2021')
gamepush.platform.call_native_sdk("lb:getLeaderboardEntries", "leaderboard2021", nil, function(result)

-- Get leaderboard data with parameters
-- equivalent to lb.getLeaderboardEntries('leaderboard2021', {quantityTop: 10, includeUser: true, quantityAround: 3})
local parameters = {
{ quantityTop = 10, includeUser = true, quantityAround = 3 }
gamepush.platform.call_native_sdk("lb:getLeaderboardEntries", parameters,function(result)

The above code is equivalent to JS code:

YaGames.init().then((ysdk) => {
// Get Yandex environment variables
let environment = ysdk.environment;

// Get the language of the Yandex.Games interface in ISO 639-1 format
let language = ysdk.environment.i18n.lang;

// Get leaderboards
ysdk.getLeaderboards().then(function (lb) {
// Record a new high score
lb.setLeaderboardScore('leaderboard2021', 120);
// Get leaderboard data
lb.getLeaderboardEntries('leaderboard2021').then(function (result) {

// Get leaderboard data with parameters
let parameters = { quantityTop: 10, includeUser: true, quantityAround: 3 };
lb.getLeaderboardEntries('leaderboard2021', parameters).then(function (result) {

Stub for platforms other than HTML

Stubs are provided for convenience in debugging on platforms other than HTML.

When using functions like:

  • player.get(key)
  • player.set(key, value)
  • player.add(key, value)
  • player.toggle(key)
  • player.has(key)
  • player.to_json()
  • player.from_json(player)

Data will be locally saved/retrieved using to/from the file "gamepush.dat" (which can be changed)

local mock_api = require("gamepush.mock_api")

-- set the name for the file to store data locally
mock_api.file_storage = "my_storage.dat"

-- setting parameters for "mocks

Author: Megalanthus

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!