Sharp Aion2 Lua SDK
API reference

scriptLua table

Run background work, pause it, and save module values.

The script table is available in every Lua script and is also exposed as sdk.script. Background workers and saved values belong to a loaded module; module lifecycle calls work from both modules and instant scripts. See lifecycle for callback timing.

script.start_worker(fn)

Parameters: fn is a function that performs work and can call script.sleep. Start a worker from module top-level code, a callback, or a cooperative coroutine in the same module state. Only one worker can be active per module; another can start after it completes. Instant scripts cannot start workers.

Returns: true when started, or nil, error if no worker can start or one is already running. The worker stops when its module unloads.

function on_start()
    local ok, err = script.start_worker(function()
        while true do
            local level = game.local_level()
            if level then log.info("Level " .. level) end
            script.sleep(2000)
        end
    end)
    if not ok then log.warn(err) end
end

script.sleep(milliseconds)

Parameters: milliseconds is an integer from 0 to 3,600,000.

Returns: Pauses the current worker before its next step. Calling it outside a worker raises an error.

function on_start()
    local ok, err = script.start_worker(function()
        log.info("Waiting")
        script.sleep(500)
        log.info("Resumed")
    end)
    if not ok then log.warn(err) end
end

script.traceback(message)

Parameters: message is one string describing an error.

Returns: The message followed by the current Lua call stack, or nil, error for invalid arguments. Use it as an xpcall error handler to preserve the original call chain before rethrowing an error.

function on_tick()
    local ok, failure = xpcall(function()
        error("Update failed")
    end, script.traceback)
    if not ok then log.error(failure) end
end

script.state_get(key)

Parameters: key is a module value name of 1–64 letters, digits, _, or -.

Returns: The saved string, nil when no value exists, or nil, error if it cannot be read. Instant scripts receive nil, error.

local previous = script.state_get("counter")
log.info(previous or "0")

script.state_set(key, value)

Parameters: key follows the same naming rule as state_get. value is a nonempty string of at most 64 KiB.

Returns: true when saved, or nil, error on failure. Values persist across module reloads.

local ok, err = script.state_set("counter", "1")
if not ok then log.warn(err) end

script.load(id)

Parameters: id is a saved module ID (1–64 letters, digits, _, or -).

Returns: true when queued, or nil, error if id is invalid, the module does not exist, or the request queue is full. If the module is already running, it stays running without restarting. Requests take effect on the next tick.

local ok, err = script.load("mob_overlay")
if not ok then log.warn(err) end

script.reload(id)

Parameters: id is a saved module ID.

Returns: true when queued, or nil, error if id is invalid, the module does not exist, or the request queue is full. Stops the module if it is running and starts a fresh state on the next tick.

local ok, err = script.reload("mob_overlay")
if not ok then log.warn(err) end

script.unload(id)

Parameters: id is a module ID.

Returns: true when queued, or nil, error if id is invalid or the request queue is full. Stops the module on the next tick, runs its on_stop callback if present, and cancels any pending crash retry.

local ok, err = script.unload("mob_overlay")
if not ok then log.warn(err) end

script.list()

Parameters: None.

Returns: An array of {id, kind, state, running, error} records for installed modules and tracked scripts. kind is "module" or "instant"; state is "running", "stopped", "faulted", or "completed".

for _, entry in ipairs(script.list()) do
    log.info(string.format("%s (%s): %s", entry.id, entry.kind, entry.state))
end