Skip to content

Latest commit

 

History

History
260 lines (180 loc) · 6.84 KB

API.md

File metadata and controls

260 lines (180 loc) · 6.84 KB

bglx API

Every script gets a global bglx table. This is the complete surface of 0.1.0. Anything not listed here is internal and may move.

Engine2 installs a strict metatable on _G; assigning a new global from a script errors. Use locals. If you truly need a global, use rawset(_G, "name", value) (the same thing Lib.declare does).

Lifecycle

bglx.ready(fn)

Runs fn once World, Lib and Me exist. If they already exist, the call is scheduled on the next engine tick.

bglx.ready(function()
  bglx.log("player is %s", tostring(bglx.player()))
end)

bglx.ui_ready(fn)

Runs when GUISystem.instance:GetRootWindow() returns a window. Use this before building custom GUI. The default panel already does this for you.

bglx.player() / bglx.world()

Return the current Me and World globals. They are live lookups, not cached copies.

bglx.diagnostics()

Returns a table with ready, ui_ready, scripts_loaded, toggles, actions, send_listeners, send_hooked and a copy of the log counters. Useful in a debug script.

Timers

bglx.after(ms, fn)

Runs once. Returns a handle:

local handle = bglx.after(500, function() bglx.log("half a second") end)
handle.cancel()

bglx.every(ms, fn)

Runs repeatedly. The engine tick is 51.2 ms on engine2, so bglx.every(50, fn) is about as fast as a plain World.Timer(1, fn) loop. Cancel the same way.

bglx.timer is an alias for bglx.every. bglx.delay is an alias for bglx.after.

Keys, toggles, actions

Key names are the engine's strings, for example key.f1, key.num1, key.pull, key.sneak. bglx does not invent a key map; pass the name through.

bglx.key(name, fn)

Raw bind. Returns a handle with off().

bglx.key("key.f4", function()
  bglx.log("pressed f4")
end)

bglx.toggle(name, label, fn, opts)

Creates a toggle and adds a row to the default panel. fn(value, handle) is called whenever the state changes. opts:

Field Meaning
default initial value, default false
id stable id used in widget names and events, default label
local fly = bglx.toggle("key.f1", "Fly", function(on)
  -- call the game API
end, { default = false })

fly:set(true)       -- sets the value, callback runs
fly:set(false, true)-- sets the value without calling the callback
fly:get()           -- current value
fly:toggle()

name may be nil if the row should only be clicked from the panel.

bglx.action(name, label, fn, opts)

Same shape as a toggle, but no state. fn(handle) runs on the key or click.

local heal = bglx.action("key.f2", "Heal", function()
  bglx.send({ pid = "Heal" })
end)
heal:run()

Registries

bglx.toggles and bglx.actions are arrays in registration order. The menu reads them directly and listens for registry / changed events.

Events

bglx.on(name, fn)

Bind to a named event. Returns { off = fn }.

local handle = bglx.on("my.thing", function(a, b)
  bglx.log("got %s %s", tostring(a), tostring(b))
end)
handle.off()

bglx.bind is the same function. bglx.once(name, fn) removes itself before calling.

bglx.emit(name, ...)

Call every handler. Handler errors are caught and logged, so one bad listener does not stop the rest.

Built-in events:

Event Arguments When
ready none engine objects are available
ui_ready none GUI root is available
registry kind, item a toggle/action was added
changed item a toggle/action changed

Hooks

bglx.hook(target, key, wrapper)

Replaces target[key] with a wrapper. wrapper(original, self, ...) is called instead of the original and may return early, change the result, or call the original more than once. Returns { restore = fn } or nil when the method is missing / not writable.

local hook = bglx.hook(Me, "setPosition", function(original, self, position)
  return original(self, position)
end)

hook.restore()

If another mod replaces the same method after bglx.hook, restore() leaves the newer function alone instead of stomping it.

Packets

bglx.on_send(fn) / bglx.on_send(pid, fn)

Tap outgoing packets. The listener receives the packet table before the engine does, so it can edit it in place. The second argument is the optional response callback passed to sendPacket.

The hook goes on Player.sendPacket (so a new Me after a map load still uses it) and falls back to Me.sendPacket when the build gives Me its own method. It is installed only while a listener exists; the last off() puts the original function back.

local tap = bglx.on_send("SomePacket", function(packet, resp)
  packet.value = 1
end)

tap.off()

bglx.on_packet is an alias for bglx.on_send.

bglx.send(packet, resp)

Calls Me:sendPacket(packet, resp) with the usual nil/type guards. Returns true when the engine call was made without a Lua error.

Scripts

bglx.script(source, name)

Compiles and runs a string now. Returns the chunk result, or nil, error when it does not compile/run. Errors are also written to the log buffer.

bglx.script("return 40 + 2", "math")

bglx.load(module)

require with error logging. Returns the module or nil, error.

Logging

The log is quiet by default and keeps the last 240 lines in memory:

bglx.log("hello %s", bglx.version)
bglx.warn("something odd")
bglx.error("broken")
bglx.debug("only useful with --verbose")

Read or write it on demand:

for _, line in ipairs(bglx.log.recent(20)) do
  -- ...
end
bglx.log.dump("/sdcard/bglx.log")

--verbose additionally sends every line to Lib.log and print.

GUI

bglx.ui is the engine2 widget helper used by the default menu. It is public for scripts that want their own panel.

Function Notes
bglx.ui.root() root window or nil
bglx.ui.size() width, height in pixels
bglx.ui.panel(opts) Layout/Frame fallback, background colour
bglx.ui.text(parent, label, x, y, w, h, opts) StaticText/Text fallback
bglx.ui.button(parent, label, x, y, w, h, fn, opts) click binding with debounce
bglx.ui.on_click(button, fn) bind both registerEvent and subscribe
bglx.ui.set_text(widget, text) tries SetText then SetProperty("Text")
bglx.ui.set_visible(widget, visible) safe visibility toggle
bglx.ui.set_color(widget, color) {r,g,b,a}

Widget types and image names vary per game; see docs/ENGINE2-NOTES.md.

Menu

bglx.menu.open() / close() / toggle()

Controls the default panel after it has been built. bglx.menu.refresh() rebuilds the rows from the registries.

Build info

Field Meaning
bglx.version runtime version string
bglx.game game id written by the packer, e.g. g2041
bglx.author quefep
bglx.discord invite URL