Getting started
You write a small subset of Lua; it is compiled to normal IC10 that runs on any IC chip. Nothing special runs in the game: other players and servers only see IC10.
local sensor = d0 --[[@as StructureGasSensor]]
local vent = d1 --[[@as StructureActiveVent]]
local TARGET = 101
while true do
local p = sensor.Pressure
if p < TARGET - 5 then
vent.On = 1
elseif p > TARGET + 5 then
vent.On = 0
end
yield()
end
This becomes 8 lines of IC10 using 1 register. Try it in the playground: the IC10 updates as you type, and the emulator runs it with editable devices.
- Playground (this site): editor with completion, IC10 next to your Lua, emulator, examples.
- In-game mod: write Lua directly in the game's IC editor (see below).
- Without the mod: copy the IC10 from the playground (Copy button) and paste it into the chip.
In-game mod
Lua for IC10 on the Steam Workshop adds Lua as a language to the IC10 Editor mod.
- Install BepInEx and StationeersLaunchPad, subscribe to IC10 Editor and Lua for IC10.
- Open a chip (IC housing or computer), choose Lua (IC10) in the language box in the top right corner. (Another mod, StationeersLua, adds a different language called just Lua.)
- Write Lua. The compiled IC10 is shown on the right, the line/register count and errors at the bottom.
- Confirm or Export writes the IC10 to the chip.
| Option | What it does |
|---|---|
| Inline | Inline small and single-use functions (fewer lines, no jal). |
| Keep Lua | Off (default): your Lua is saved on your computer (BepInEx/config/LuaIC10/sources) and the chip gets one line # lua-src …; the chip opens as Lua again on your computer. On: the whole Lua source is stored on the chip as a compressed comment, so it opens as Lua for everyone (uses more of the 128 lines / 4096 bytes). |
| IC10 | Show or hide the IC10 preview next to the Lua. |
| chip: | For scripts with several chips: which chip's program goes on this chip. |
Hover a Lua line to see its IC10. Library scripts are saved as Lua.
Devices and pins
The pins d0–d5 are the devices wired to the housing; db is the housing itself.
local sensor = d0 --[[@as StructureGasSensor]] -- an alias: no code, no register
local tank = device(12345) -- a device by ReferenceId (ld/sd)
if d2.exists then -- is something connected to d2?
d2.On = 1
end
The --[[@as Type]] comment is optional. It tells the editors (playground, VS Code, Zed, the in-game mod) which device it is, so they can complete its properties and warn about typos. Use the prefab name from the Stationpedia.
Properties, slots, reagents
| Lua | IC10 |
|---|---|
local t = sensor.Temperature | l r0 d0 Temperature |
vent.On = 1 | s d1 On 1 |
d0.slots[0].Occupied | ls r0 d0 0 Occupied (slots start at 0, as in the game) |
d0.slots[1].Lock = 1 | ss d0 1 Lock 1 |
reagent(d0, "Contents", hash("ItemIronOre")) | lr — modes: Contents, Required, Recipe, TotalContents |
d2.exists | sdse; in an if: bdse / bdns |
Variables and constants
local x = 5uses a register (r0–r15). When registers run out, variables go to the chip's stack memory automatically.local NAME = 5— ALL_CAPS with a constant value is folded into the code: 0 registers, 0 lines.a, b = b, aworks. Globals (withoutlocal) get a permanent register.hash("StructureSolarPanel")is a constant (HASH("…")).- Operators:
+ - * / % ^ //,& | ~ << >>,< <= > >= == ~=,and or not.
Control flow and timing
if a then … elseif b then … else … end
while cond do … end
repeat … until cond
for i = 1, 10 do … end -- the step must be a constant: for i = 10, 1, -1
break, goto label / ::label::, do … end
yield() -- wait for the next tick: put one in every main loop
sleep(2) -- wait 2 seconds
hcf() -- stop the chip
A loop without yield() or sleep() runs many times per tick, exactly like in IC10.
Functions
local function clamp01(x)
return math.clamp(x, 0, 1)
end
d0.Setting = clamp01(d1.Setting)
- Small (≤ 3 instructions) and single-use functions are inlined: no
jal, no stack. - Other functions are real calls (
jal/j ra); recursion works. Only the registers still needed after the call are saved. - One return value; missing arguments are 0. Define a function before you call it.
Arrays and memory
local limits = {290, 300, 310} -- fixed array
local history = array(10) -- 10 slots set to 0 (array(n, fill))
history[i] = limits[2] + #limits
Arrays live in the chip's own stack memory (get/put db), from address 511 down. Indexes start at 1 like Lua. #t is a constant.
Batch (all devices of a type)
local PANELS = hash("StructureSolarPanel")
batch.write(PANELS, "Horizontal", 90) -- sb
local avg = batch.read(PANELS, "Charge") -- lb, mode Average
local sum = batch.read(PANELS, "Charge", "Sum") -- Average, Sum, Minimum, Maximum
batch.write_named(PANELS, hash("North"), "On", 1) -- sbn
local n = batch.read_named(PANELS, hash("North"), "Charge", "Maximum") -- lbn
-- slots of every device of a type
local full = batch.read_slot(PANELS, 0, "Occupied", "Sum") -- lbs
local f2 = batch.read_named_slot(PANELS, hash("North"), 0, "Occupied") -- lbns
batch.write_slot(PANELS, 0, "Lock", 1) -- sbs
Batch operations reach every device of that type on the chip's data network.
math and built-ins
| Lua | IC10 |
|---|---|
math.abs floor ceil sqrt exp log | abs floor ceil sqrt exp log |
math.sin cos tan asin acos, math.atan(y, x) | sin cos tan asin acos atan / atan2 |
math.max(a, b, …), math.min(…) | max, min |
math.random(), math.pi | rand, constant |
math.round, math.trunc, math.sign | round trunc sgn (extensions) |
math.clamp(x, lo, hi), math.lerp(a, b, t) | clamp, lerp |
math.isnan(x), math.approx(a, b, tol?) | snan, sap |
math.rad(deg), math.deg(rad) | mul by deg2rad / rad2deg |
math.huge, -math.huge, math.nan | pinf, ninf, nan |
math.tau epsilon deg2rad rad2deg rgas | IC10 constants (free) |
bit.band bor bxor nor bnot | and or xor nor not (also & | ~) |
bit.sll srl sla sra rol ror | shifts and rotations (also << >>) |
bit.extract(x, off, len), bit.replace(x, v, off, len) | ext, ins |
str("HELLO") | STR("HELLO"): up to 6 characters in one number (LED displays) |
Color.Red, GasType.Oxygen, Sound.Alarm2, SlotClass.Ore… | game enums as constants (26 groups, completion in the editor) |
rmap(d0, hash("ItemIronOre")) | rmap |
clear(d0), clear(device(id)) | clr, clrd |
select(cond, a, b) | select (cond ? a : b) |
Several chips
One script can describe several chips that work together, with shared variables:
shared = { temp = 0, alarm = 0 }
chip("atmos", function()
local sensor = d0 --[[@as StructureGasSensor]]
while true do
shared.temp = sensor.Temperature
shared.alarm = shared.temp > 310
yield()
end
end)
chip("display", { link = d5 }, function()
local screen = d0 --[[@as StructureConsoleLED5]]
while true do
screen.Setting = shared.temp
yield()
end
end)
- Every
chip(…)is its own program with its own 128 lines and 16 registers. sharedlives in the first chip's stack. The other chips need{ link = d5 }(the pin wired to the first chip's housing) or{ link = 12345 }(its ReferenceId).- In the mod, pick which chip goes on the housing with the chip: box.
Differences from Lua
- 0 is false. Everything is a number:
if x thenmeansx ~= 0;true/false/nilare 1/0/0. - Numbers only. Strings only as arguments:
hash("…"), property names and modes inbatch.*andreagent(). - Tables only as fixed arrays and
shared. Noprint,string,table, closures as values or.... - Limits: the result must fit in 128 IC10 lines. The editors show the count as you type.
Cheat sheet
| Want | Write |
|---|---|
| Name a device | local pump = d2 --[[@as StructureVolumePump]] |
| Read / write | local x = pump.Setting / pump.On = 1 |
| Main loop | while true do … yield() end |
| Hysteresis | if p < LOW then x.On = 1 elseif p > HIGH then x.On = 0 end |
| All panels | batch.write(hash("StructureSolarPanel"), "Vertical", v) |
| Is it plugged in? | if d3.exists then … end |
| Item in a slot | d0.slots[0].Occupied |
| Wait | yield() (1 tick), sleep(seconds) |