Defold
The Gamealia library for Defold is a Lua module, gamealia.gamealia, over the JavaScript SDK. It is a plain library project: no native extension, so no build server is involved, and no custom HTML template. In an HTML5 bundle it loads the SDK, runs init(), calls loadingStop() for you, mutes the game while the portal asks for it, and gives you Lua functions for ads, player accounts, cloud save, rooms, friends and leaderboards. On other platforms (the editor's desktop build) every call is a no-op and ads resolve as not shown, so the same code runs everywhere.
Tested with Defold 1.14.1: the sample project, bundled for HTML5 with bob.jar, passes the autotest (SDK loaded, init, loading and gameplay calls, locale).
Install
- Open
game.project> Project > Dependencies and addhttps://gamealia.app/downloads/gamealia-defold.zip(in the file:dependencies#0 = https://gamealia.app/downloads/gamealia-defold.zip, or the next free number). - Project > Fetch Libraries. The
gamealiafolder appears in the Assets pane. gamealia init --writein the project folder adds the dependency line for you.
Off the portal (a local HTML5 bundle served from your computer) the SDK runs in mock mode: fake ads, a local save, nothing is sent.
Minimal flow
local gamealia = require("gamealia.gamealia")
function init(self)
gamealia.set_listener(function(self, event, data)
if event == "locale_change" then update_texts(gamealia.locale()) end
if event == "pause" then self.paused = true end
if event == "resume" then self.paused = false end
end)
-- init() also calls loading_stop() once the SDK is ready.
gamealia.init(function(self, info, err)
update_texts(gamealia.locale()) -- "en", "es", "pt-BR"...
gamealia.gameplay_start()
end)
end
local function on_game_over(self)
gamealia.gameplay_stop()
gamealia.request_midgame(function(self, result)
-- continue: back to the menu, next round...
end)
end
local function on_reward_button(self)
gamealia.request_rewarded(function(self, result)
if result.rewarded then give_reward(self) end
end)
end
Callbacks are function(self, result, err): self is the script that called gamealia.init() (call it from your main script), err is nil or a message. While an ad plays the portal sends pause and mute: stop your game in the listener (the sound is muted for you).
If your game keeps loading after its first frame (your own loading screen), use gamealia.init(cb, { manual_loading_stop = true }) and call gamealia.loading_stop() yourself.
Functions
| Area | Lua |
|---|---|
| Start | init(cb, opts), info(), locale(), environment(), is_mobile(), join_params(), set_listener(fn) |
| Game | loading_start(), loading_stop(), gameplay_start(), gameplay_stop(), happy_time(), set_progress(percent), set_context(tbl) (nil clears it), report_error(message), track(category, what, action) |
| Ads | request_midgame(cb), request_rewarded(cb) (result {shown, rewarded, reason}), has_adblock(cb), request_banner(id, size, anchor_x, anchor_y, pos_x, pos_y, cb) and request_responsive_banner(id, anchor_x, anchor_y, pos_x, pos_y, max_width, max_height, cb) (result {ok, reason, size}; in-game banners drawn by the portal over the game, shown once ads are live on Gamealia, until then reason "not-live"), clear_banner(id), clear_all_banners() |
| Player account | user(), get_user(cb), get_token(cb), request_login(cb) |
| Cloud save | data_load(cb), data_get(key), data_set(key, value, cb), data_remove(key, cb), data_clear(cb) |
| Rooms and invites | room_get_join_code(), room_get_join_key(), room_get_join_pass(code, cb), room_join_failed(reason, code, cb), room_invite_link(code, cb), room_show_invite(code, cb), room_publish(room, cb), room_unpublish(cb), set_presence(text, cb) |
| Friends | friends_list(cb), friends_invite(id, code, cb) |
| Leaderboards | leaderboard_submit(board, value, cb), leaderboard_get(board, opts, cb), leaderboard_show(board, cb) |
| Any call | call(path, args, cb), for example gamealia.call("game.setProgress", {40}) |
Listener events: locale_change (the new locale), ad_start, ad_end, pause, resume, mute, unmute, user_change (the player or nil), room_join ({code, key}) and friends_change. info() holds the whole init() result: environment, locale, gameId, user, device, deviceClass, country, app, joinRoom, joinKey, joinParams, instantMultiplayer, chatOff, os and browser ({name, version}; also os_name(), os_version(), browser_name(), browser_version()).
Each function maps to the JavaScript SDK call of the same name (SDK reference). A few details:
- Cloud save: run
data_load(cb)once (for example in theinitcallback), then readgamealia.data_get("key"). Numbers, strings, booleans and Lua tables are saved as they are. - Player accounts: guests must be able to play.
get_tokengives a token for your own server; see Player accounts. - Rooms: when
room_get_join_code()is notnilat start (or onroom_join), join that room in your own netcode; see Multiplayer. - Leaderboards: boards are set up in the dashboard; see Leaderboards.
Audio mute
On mute the library sets the "master" sound group gain to 0 and restores the previous gain on unmute. If you route sound another way (or want your own fade), set gamealia.auto_mute = false and handle mute / unmute in the listener.
JavaScript-only features
Chat, store and the other extras of the SDK reference have no Lua functions. Call them with html5.run, which runs JavaScript in the page next to window.Gamealia:
html5.run("Gamealia.chat.join('global')")
html5.run returns synchronously, so to get a result back keep it in a page variable and read it with a later html5.run.
Bundle
- Project > Bundle > HTML5 Application (or
java -jar bob.jar --platform wasm-web --archive resolve build bundlewith--bundle-output <folder>). - Upload the bundle folder (the one with
index.html,dmloader.js, the.wasmandarchive/), or zip it as it is.
The library loads https://gamealia.app/sdk/v1.js itself; you do not need a custom html5.htmlfile. Adding the script tag to your template is fine too (gamealia init adds it to a bundle's index.html).
Already use another portal's Defold extension?
Those extensions are native extensions that load their own SDK, so our compatibility shim cannot take their place. Replace the dependency with the Gamealia library and move each call to the Gamealia one (same idea: loading, gameplay start and stop, midgame and rewarded ads); the migration guides list the equivalent calls.
Common autotest failures
gameplay_start/gameplay_stop:gamealia.gameplay_start()/gameplay_stop()are never called after the first tap or key.loading_stop:manual_loading_stop = trueandloading_stop()is never reached.sdk_loaded:gamealia.init()is never called (the library loads the SDK on first use).translations: texts set once fromgamealia.locale()without handlinglocale_change.mute_response: sound that does not go through the"master"group keeps playing.
Problems or questions: the Support chat in your dashboard, or hello@gamealia.app (Support).