Pin a sidecar link by index¶
Goal: bind an mforth link name to a processor's link-slot number
N(the ordergetlinkreturns it in) instead of the in-game block label. You accept fragility to in-game re-link order in exchange for stability across rebuilds when that order is preserved.Prerequisites:
- mforth installed and on
$PATH(see Install).- A
.fssource you want to bind to one or more linked blocks.- Knowledge of the in-game link order for your processor — open the processor in Mindustry; the link list is numbered top-to- bottom starting at
0, matching what thegetlinkinstruction returns.When NOT to use this. Tutorials and most real programs should use
target = "<in-game-name>"instead.index = Nis fragile: unlink + relink the blocks in a different order and yourdisplayhandle now points at the switch you used to callgate. Reach forindexonly when the in-game name is not reliably preserved across rebuilds — for example, a display block that gets destroyed and replaced during play, where Mindustry assigns a freshmessage1/message2/ … label each time. Re-read the sidecar schema reference for the full shape of[links.X]before continuing.
Steps¶
-
Pick the slot. In Mindustry, click the processor and read the numbered link list. The slot you want is whatever
getlink Nwould return —0for the first link,1for the second, and so on. Out-of-range slots resolve tonullin mlog andNonein the host REPL. -
Write the sidecar entry. In
<name>.world.toml, declare the link withindex = Ninstead oftarget = "...". The left side of=is still the stable mforth-name your.fssource references; theindexonly binds it to a concrete slot.# display.world.toml — display is processor link slot 0. # WARNING: index-mode. If you re-link the processor and slot 0 # ends up bound to something other than the message block, this # script will print to the wrong block. Re-pin (or switch to # `target = "..."`) when that happens. [links.display] type = "message" index = 0targetandindexare mutually exclusive — declaring both, or neither, is a parser error. See the Troubleshooting section below for the exact messages. -
Compile and inspect the prologue. Run
mforth compileand confirm agetlink <mforth-name> <N>instruction sits at the top of the emitted mlog, ahead of the first user-visible instruction:For the sidecar above plus a one-liner
display PRINTFLUSH, you should see:# mforth output — 2 instructions; SOURCE=display.fs; SIDECAR=display.world.toml getlink display 0 printflush displayThe
getlink display 0line is the Mode B prologue: it resolves slot0into thedisplayvariable once, so every later use ofdisplayreads the same in-game block handle. Onegetlinkper index-bound link, emitted in sidecar-declaration order. -
Paste and run in Mindustry. Copy
display.mloginto the processor as usual. The prologue executes once per auto-loop iteration; if slot0is bound to the block you expected, the program behaves exactly as the host REPL did against theindex = 0sidecar. -
Re-pin when the in-game link order changes. If you unlink and re-link the blocks, the slot numbers shift. Open the processor, note the new order, update
index = Nin the sidecar to match, and recompile. There is no in-game indicator that an index has drifted — the script will simply talk to the wrong block until you re-pin or migrate totarget = "...".
Troubleshooting¶
-
My script bound to the wrong block. Re-linking in a different order is the usual cause. Open the processor in-game, count down the link list to slot
N, and confirm it points at the block you meant. If the in-game block has a stable label, consider switching the sidecar totarget = "<in-game-name>"instead — that mode survives re-link order changes. -
Parser error:
cannot specify both 'target' and 'index' — exactly one is required. Your[links.X]entry has both keys. Drop one. If you were migrating from one mode to the other and forgot to delete the old line, that is almost certainly what happened. -
Parser error:
requires exactly one of 'target' or 'index' (neither was given). Your[links.X]entry hastype = "..."but no binding. Add eithertarget = "..."(recommended) orindex = N(this how-to). -
Parser error:
[links.X].index must be an integer. TOML quoted the value —index = "0"is a string, not an int. Drop the quotes:index = 0. -
No
getlinkprologue appears in the mlog output. Confirm the compile picked up the sidecar —mforth compilelooks for<name>.world.tomlnext to<name>.fsautomatically. The mlog output header listsSIDECAR=<path>when one was loaded; if the header saysSIDECAR=<none>, the file is not where the compiler expected it or has a different basename.
See also¶
- Sidecar schema (
.world.toml) — the full reference for[links.X], including every error condition the loader raises. - Why mforth — the REPL ↔ mlog equivalence rule the sidecar exists to serve.
- Use with Helix /
Use with Neovim — once an index-mode link
is wired, the LSP resolves
display(and friends) the same way for hover and completion as it does fortarget-mode links.