> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gamavas.fun/llms.txt
> Use this file to discover all available pages before exploring further.

# Defold

> Gamavas Pass for Defold's HTML5 build: a pure Lua library for sign-in, achievements and selling.

A Defold library for the **HTML5** build. It's pure Lua with no native extension, so it doesn't need Defold's build server. On desktop and mobile builds, calls report `unsupported`.

## Install

**1. Add the dependency**

In `game.project`, under **Project → Dependencies**, add:

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
https://github.com/Gamavas/pass-defold/archive/refs/tags/v0.2.0.zip
```

Then choose **Project → Fetch Libraries**.

**2. Register your page**

At [developer.gamavas.fun](https://developer.gamavas.fun), connect your listing to get a client ID, and add your game's page under **Where sign-in returns**. To test from Defold (**Project → Build HTML5**), add the local address it opens too, for example `http://localhost:51234/` (your port).

## Sign players in

Start once, from a script's `init()`. That script's timer delivers the answers, so use one that lives as long as your game:

```lua theme={"theme":{"light":"github-light","dark":"github-dark"}}
local gamavas = require("gamavas.pass")

function init(self)
    msg.post(".", "acquire_input_focus")
    gamavas.init("YOUR_CLIENT_ID")
    gamavas.on_player_changed(function(player)
        print(player and ("Hi @" .. player.handle) or "Signed out")
    end)
end

function on_input(self, action_id, action)
    if action_id == hash("touch") and action.pressed then
        gamavas.sign_in(function(r)                 -- from a click or tap
            if r.ok then print("Hi @" .. r.value.handle) else print(r.error, r.message) end
        end)
    end
end
```

Every call reports to your callback with a table: `{ ok, value, error, message }`. Once `gamavas.is_ready()` is true, `gamavas.is_signed_in()` and `gamavas.player()` say who's signed in.

## Achievements, progress and selling

```lua theme={"theme":{"light":"github-light","dark":"github-dark"}}
gamavas.unlock("first-win", function(r) end)                 -- shows the "Achievement unlocked" notice
gamavas.unlock("secret", { notify = false }, function(r) end)  -- no notice
gamavas.achievements(function(r) for _, a in ipairs(r.value or {}) do print(a.name, a.unlocked) end end)
gamavas.progress(function(r) if r.ok then print("Level " .. r.value.level) end end)

gamavas.owns(function(r) if r.ok and r.value then start_game() end end)
gamavas.purchase(function(r) if r.ok and r.value then start_game() end end)   -- from a click
```

## Faster sign-in popup (optional)

The library loads the Gamavas script itself. To make the sign-in popup close before your game starts loading in it, copy Defold's `builtins/manifests/web/engine_template.html` into your project, set it as **HTML5 → Html File** in `game.project`, and add this to its `<head>`:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script src="https://gamavas.fun/sdk/pass.js"></script>
```

## Your server

```lua theme={"theme":{"light":"github-light","dark":"github-dark"}}
gamavas.access_token(function(r) if r.ok then send_to_server(r.value) end end)
```

Verify it on your server with [`@gamavas/pass-server`](https://docs.gamavas.fun/sdks/server).

## Options

```lua theme={"theme":{"light":"github-light","dark":"github-dark"}}
gamavas.init("YOUR_CLIENT_ID", {
    test = false,       -- test purchases for you and your testers; off before you ship
    analytics = true,   -- play time for your Analytics page; never earns XP
    issuer = "https://staging.gamavas.fun/api/auth",   -- only for testing against staging
})
```

## Errors

`error` is one of `popup_closed` or `access_denied` (signing in), `popup_blocked`, `signed_out`, `forbidden`, `not_found`, `rate_limited`, `network`, `not_initialized` (call `gamavas.init` first) or `unsupported` (not an HTML5 build).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.