Taps and pages
Make the companion tappable, with detail pages, buttons, and pages the game opens by itself.
Any widget on the companion screen can react to a tap with onClick. A pages widget
shows one screen at a time, so a tap can open a detail page and a button can go back.
Your Lua script sees which page is open and what was tapped, so each page gets the data
it needs.
Taps never take the controller away from the game.
A party list that opens a detail page
{ "type": "pages", "pages": [
{ "name": "party", "child":
{ "type": "list", "items": "$party", "template":
{ "type": "row", "class": "mon",
"onClick": [ { "set": { "selected": "{@index}" } }, { "navigate": "detail" } ],
"children": [ { "type": "text", "text": "{name}" } ] } } },
{ "name": "detail", "child":
{ "type": "column", "children": [
{ "type": "button", "text": "‹ Back", "onClick": "back" },
{ "type": "text", "text": "{detail.name}" },
{ "type": "list", "class": "chips", "items": "$detail.weak", "template":
{ "type": "text", "class": "chip", "text": "{name} {label}" } }
] } }
] }Tapping a row saves its position as selected and opens the detail page.
"‹ Back" returns to the party list. The script fills in detail, as shown in
Feeding a page from Lua.
GBA Duo's party-css example
has a complete version: tap a party member for its stats, weaknesses and resistances.
UI state
The screen keeps a small set of values called the UI state:
- which page each
pageswidget shows (pageby default); - anything a tap
sets.
Layouts read it as $ui.<key> (for example "{ui.selected}" or
"visibleIf": "$ui.showAll"). The script reads it as ui.<key>. It starts empty,
apart from each pages widget's initial page, whenever a pack is loaded. Values are
strings, numbers or booleans.
onClick
Every widget accepts onClick: one action, a list of actions, or "back".
| Action | Does |
|---|---|
{ "set": { "selected": "{@index}" } } |
Sets UI keys. A value that is exactly one {...} keeps its type ({@index} stays a number); other strings are filled in as text. |
{ "navigate": "detail" } |
Shows that page and remembers the current one for back. |
{ "back": true }, or "back" |
Returns to the previous page (the initial page if there's no history). |
{ "call": "heal", "args": ["{@index}", "{name}"] } |
Runs actions.heal(index, name) in the pack's Lua (below). |
{...} placeholders resolve like everywhere else in a layout: in a list item, the
item's fields, then @index, then the whole snapshot; anywhere, ui.*.
One object can combine actions. They run in the order set, navigate/back,
call. A list runs in order. UI changes from a tap show at once; a call runs on the
next frame.
"state" picks which pages widget navigate and back drive (page by default):
{ "navigate": "moves", "state": "tab2" }.
While a widget with onClick is held down it matches :pressed, so you can style it:
.mon:pressed { transform: scale(.97); }pages
{ "type": "pages", "state": "page", "initial": "party", "pages": [
{ "name": "party", "child": { ... } },
{ "name": "detail", "child": { ... } }
] }| Field | Default | |
|---|---|---|
pages |
(required) | name + child for each page; names must be unique |
state |
page |
The UI key holding the shown page's name |
initial |
the first page | Shown until something navigates; also where back ends |
- Only the shown page is laid out and drawn. Hidden pages stay bound to the data, so they're up to date when opened.
- A page name that doesn't exist shows the initial page.
- Several
pageswidgets can share astateor use their own. - Selectors:
pages, andpages > *for the page roots. Give pages aclassoridto style them.
button
{ "type": "button", "text": "Heal all", "onClick": { "call": "healAll" } }A box laid out as a centered row with default padding, a rounded background, and
:pressed feedback. text becomes a text child with the class label
(button > .label). children can add more, such as an icon. Any other widget with
onClick works too; button just comes styled.
Zoom (maps)
A box with zoom lets the player look closer: pinch to zoom, drag with one finger to pan (once zoomed in),
and + / - buttons for the same thing. The box stays where it is and clips; its own background stays still;
everything inside it scales. Taps on what's inside still work (a pin, a room).
{ "type": "box", "class": "mapwin", "zoom": { "min": 1, "max": 4, "state": "mapZoom" }, "children": [ ... ] },
{ "type": "button", "class": "zoom-btn", "onClick": { "zoom": "out", "state": "mapZoom" }, "children": [ { "type": "text", "text": "-" } ] },
{ "type": "button", "class": "zoom-btn", "onClick": { "zoom": "in", "state": "mapZoom" }, "children": [ { "type": "text", "text": "+" } ] }zoom:true(1x to 4x, UI keyzoom) or{ "min", "max", "state" }.- The scale is UI state:
$ui.mapZoomin bindings,ui.mapZoomin Lua (a number,minuntil zoomed). A pinch writes it when the fingers lift;{ "set": { "mapZoom": 2 } }jumps to 2x. - The
zoomaction:"in"(x1.5),"out"(/1.5) or"reset"(back tomin), clamped to the box's limits. Buttons zoom around the middle of the box; a pinch around the fingers. The content can't be dragged past its edges. - A
zoomaction stands alone in its object (put it in a list with other actions). - Needs app 1.0.3 or newer. Older apps ignore
zoom(the map shows at 1x, the buttons do nothing), so a pack that uses it sets"recommendedAppVersion": "1.0.3"in itscompanions.jsonentry rather thanminAppVersion.
Feeding a page from Lua
update() receives the UI state, which is also the global ui. Use it to compute only
what the open page needs:
local typechart = pack.require("typechart")
function update(ui)
local party = readParty() -- your pack's own memory reading
local out = { party = party }
if ui.page == "detail" and ui.selected then
local mon = party[ui.selected + 1] -- @index counts from 0, Lua from 1
if mon then
local weak, resist, immune = typechart.matchups(mon.types)
out.detail = {
name = mon.name, level = mon.level, hp = mon.hp, maxHp = mon.maxHp,
weak = weak, resist = resist, immune = immune,
}
end
end
return out
endWhere the data comes from is up to the pack:
- Game RAM: party stats, moves, HP.
- ROM:
mem.*reads the cartridge too, for example species base stats. - Data files in the pack:
.luamodules loaded withpack.require, like a type chart, or notes from a guide whose license allows it.
A tap that changes the UI state triggers a fresh update(ui) right away, so a detail
page fills in almost instantly instead of waiting for the next tick. This also works
while the game is paused.
Changing pages from update()
The game itself can switch pages, without a tap: return a ui entry from update().
It holds UI actions in the same shape as an onClick (set, navigate, back; not
call) and runs right after the rest of the returned data is applied. For example,
open a battle page while a battle is on:
local wasInBattle = false
function update(ui)
local inBattle = mem.u8(BATTLE_FLAG) ~= 0 -- whatever your game's flag is
local out = { party = readParty() }
if inBattle then out.battle = readBattle() end
if inBattle and not wasInBattle then
out.ui = { navigate = "battle" } -- the battle just started
elseif wasInBattle and not inBattle then
out.ui = { back = true } -- it ended: back to the previous page
end
wasInBattle = inBattle
return out
end- Return
uionly when something changes, as above, so players can still tap away while it lasts. Returning{ navigate = "battle" }every tick is harmless but sends them back to it each tick. backpops one page of history each time it's returned, so return it once.uiisn't passed to the layout as data:$ui.*is always the UI state.- It takes effect at the next snapshot, so within one tick.
Script actions
{ "call": "name", "args": [...] } runs actions.name(...) from the pack's script:
actions = {}
function actions.open(index)
-- Decide in Lua which page to show.
if index == 0 then return { navigate = "lead" } end
return { set = { selected = index }, navigate = "detail" }
end- Arguments: strings, numbers and booleans; anything else arrives as
nil. At most 16. - Return value: a table in the same shape as an
onClickaction (set,navigate,back), applied to the screen. Return nothing to leave the screen as it is. A returnedcallis ignored; call the Lua function directly. - Timing: actions run on the emulator thread just before the next
update(ui), with the same limits asupdate: 1M instructions and 25 ms per call. - Errors: an error or a missing function is logged (
adb logcat -s EmuThread) and the screen stays as it was.
Changing the game
An action can also write to the game's memory: equip an item from the companion screen, or set a value without opening a menu. Reading needs nothing. Writing needs a permission in the manifest, and the player's say-so.
{
"formatVersion": 3,
"permissions": ["memory-write"]
}function actions.equip(button, itemId)
if mem.u8(SAFE_FLAG) == 1 then
local ok, why = mem.write8(B_SLOT, itemId)
if not ok then return { set = { message = "Couldn't equip: " .. why } } end
end
endThe write functions return true, or false and a reason,
so the pack can say why on the companion screen. Nothing is written when they return
false.
The checks live in the app, not in the pack's Lua, so a pack can't skip them:
- Permission. Without
"permissions": ["memory-write"](formatVersion3) every write is refused. An unknown permission makes the manifest invalid. - Only from a tap. Writes work only while an
onClickaction runs.update(),init()and the script's top level can't write, so a pack can't change the game on its own. A function saved during one tap can't write later. - Only RAM. GBA EWRAM
0x02000000-0x0203FFFFand IWRAM0x03000000-0x03007FFF; GB/GBC work RAM0xC000-0xDFFFand high RAM0xFF80-0xFFFE. ROM, I/O registers, video memory, the save and mirrors of RAM are refused. A write must fit inside one region. - Size. At most 64 bytes per call and 256 bytes per tap.
- The player. Writing is off until the player turns on Settings › Packs can change games. The first time a pack is about to change a game, GBA Duo asks; the answer is kept for that pack and game, and asked again if the pack's code changes. If they allow it, a save state is made just before.
- Timing. Writes happen on the emulator thread between frames, so they never race the emulator. The game sees them on its next frame.
- Logged. Every write is logged with its address and size:
adb logcat -s Companion.
Writes don't roll back: if an action writes and then errors, what it wrote stays. Write last, after every check.
Find real addresses before you write anything, and check a "safe to act" condition
first: a wrong write can crash the game or spoil the save. The app repo has a complete
example,
zelda-items,
with notes on finding addresses.