← Back to home

Defold SDK

Integrate Asobi into your Defold project. Pure Lua, no native extensions required.

View on GitHub

Installation

Add as a library dependency in your game.project:

[project]
dependencies = https://github.com/widgrensit/asobi-defold/archive/main.zip

Or: Project → Fetch Libraries after adding the URL.

Setup

local asobi = require("asobi.client")

function init(self)
    self.client = asobi.create("localhost", 8080)
end

Authentication

All HTTP calls use callbacks with (data, error) signature:

-- Register
self.client.auth.register(self.client,
    "player1", "secret123", "Player One",
    function(data, err)
        if err then
            print("Error: " .. err.error)
            return
        end
        print("Registered as: " .. self.client.player_id)
    end)

-- Login
self.client.auth.login(self.client,
    "player1", "secret123",
    function(data, err)
        if not err then
            print("Logged in!")
        end
    end)

Real-Time Connection

Connect via WebSocket and subscribe to events:

-- Subscribe to events
self.client.realtime.on("connected", function()
    print("Connected!")
end)

self.client.realtime.on("match_state", function(payload)
    -- Update game state
end)

self.client.realtime.on("matchmaker_matched", function(payload)
    self.client.realtime.join_match(payload.match_id)
end)

-- Connect
self.client.realtime.connect()

Matchmaking

-- Queue via WebSocket
self.client.realtime.add_to_matchmaker("arena")

-- Or via REST
self.client.matchmaker.add(self.client, "arena",
    function(data, err)
        print("Ticket: " .. data.ticket_id)
    end)

Match Input

-- Send input (fire-and-forget)
self.client.realtime.send_match_input({
    action = "move",
    x = 1,
    y = 0
})

-- Leave match
self.client.realtime.leave_match()

Leaderboards

-- Submit a score
self.client.leaderboards.submit_score(self.client,
    "weekly", 1500, 0,
    function(data, err)
        print("Score submitted!")
    end)

-- Get top scores
self.client.leaderboards.get_top(self.client,
    "weekly", 10,
    function(data, err)
        for _, entry in ipairs(data.entries) do
            print(entry.player_id .. ": " .. entry.score)
        end
    end)

Chat

-- Listen for messages
self.client.realtime.on("chat_message", function(payload)
    print(payload.content)
end)

-- Join channel and send message
self.client.realtime.join_chat("lobby")
self.client.realtime.send_chat_message("lobby", "Hello!")