Skip to main content

Adding a Plugin to a Defold Project

Installation

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

Initialization

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

[gamepush]
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")

gamepush.init(function(success)
if success then
-- initialization was successful
else
-- initialization error
end
end)

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
end

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

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

-- event functions can be assigned before SDK initialization
gamepush.ads.callbacks.start = ads_start
gamepush.ads.callbacks.close = ads_close
gamepush.ads.callbacks.rewarded_reward = ads_reward
-- SDK initialization
gamepush.init(function(success)
if success then
-- show the preloader
gamepush.ads.show_preloader(function(result)
-- ad is closed, we can do something
end)
-- we do something else

-- show rewarded video ad
gamepush.ads.show_rewarded_video()

-- show rewarded video ad using a callback
gamepush.ads.show_rewarded_video(function(result)
if result then
-- give the player some crystals
end
end)
else
-- initialization error
end
end)

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
environmentobjectcall_native_sdk("environment")
table
environment.i18n.langstringcall_native_sdk("environment.i18n.lang")
string
envundefinedcall_native_sdk("env")
{error = 'Field or function "env" not found!'}
player.getUniqueID()functioncall_native_sdk("player.getUniqueID")
string
feedback.canReview()functioncall_native_sdk("feedback.canReview", nil, callback)
nil
Callback will be called upon promise completion.
getLeaderboards().then(lb => {})functioncall_native_sdk("lb=getLeaderboards", nil, callback)
nil
Callback will be called upon promise completion.
The result will be saved in a JS variable.
lb.setLeaderboardScore()functioncall_native_sdk("lb:setLeaderboardScore")
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")

gamepush.init(function(result)
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)
pprint(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)
pprint(result)
end)

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

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) {
console.log(lb);
// Record a new high score
lb.setLeaderboardScore('leaderboard2021', 120);
// Get leaderboard data
lb.getLeaderboardEntries('leaderboard2021').then(function (result) {
console.log(result);
});

// Get leaderboard data with parameters
let parameters = { quantityTop: 10, includeUser: true, quantityAround: 3 };
lb.getLeaderboardEntries('leaderboard2021', parameters).then(function (result) {
console.log(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 sys.save()/sys.load() 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!