Skip to content

Code mods

Lua scripts listed in mod.json "scripts". They load only with MOD CODE on (see How mods work).

-- mods/scrapyard/main.lua  (mod.json: "scripts": ["main.lua"])

-- Content, exactly like the JSON files:
game.add('enemies', {id = 'scrapyard.truck', base = 'patrol',
  hp = 40, spawn = {heat = 3, every = 25}, behavior = 'scrapyard.truck'})

-- Behaviour the rows name:
game.enemy('scrapyard.truck', {
  think = function(s, e, dt)
    if game.distance(e, s.player) < 140 then
      game.drive(s, e, s.player.x, s.player.y, 480, dt)
      return true                       -- replaces the base driving this tick
    end
  end,
  death = function(s, e) game.blast(s, e.x, e.y, 48, 200) end,
})

-- A difficulty with a rule only code can express:
game.effect('scrapyard.leaky_tank', {tick = function(s, dt) game.nos(s, -0.02 * dt) end})
game.add('difficulties', {id = 'scrapyard.leaky', numeral = 'L', name = 'LEAKY TANK',
  short = 'LEAKY NOS', rule = 'YOUR NOS TANK SLOWLY DRAINS', payout = 1.5,
  effect = 'scrapyard.leaky_tank'})

Rules

  • Call game.add and game.change at the top level. Rows are checked when the mod finishes loading.
  • Handlers get the run state s first. Run state lists the stable fields.
  • dt and run timers use world seconds. Normal speed schedules 51 world ticks per real second, each advancing 1/60 world second. This is the default 1x pace; multiply rates by the supplied dt. Drawing and menu animation use their own presentation clock, so do not count display frames to measure a gameplay duration.
  • Use game.random, not math.random. It has its own seeded stream per run, so runs stay reproducible.
  • A handler that errors is switched off for the session; the run continues. The mod reads ERROR on the MODS page, with the message, and the full error is in mods.log (see When something breaks). A script that fails while loading skips its mod.
  • Require game modules (game.upgrades, game.vehicles...) inside handlers, not at the top: content is still loading while scripts run.

Events

game.on(event, fn) hears what happens in every run. The game publishes these besides tick and the draw layers:

Event Gets When
enemy_spawn the enemy Any enemy joins the run
enemy_death the enemy A takedown: shot, blasted, crashed or drowned
enemy_gone the enemy It left without a takedown (drove off, cleared)
player_hit hearts lost Each hit on the player
heat the new level The wanted level rose
card the card id A card was bought at a laundromat
pickup {kind, amount, x, y} Cash was picked up or a pit stop filled the BOOST
escaped The run ended as an escape
key modid.setting A mod's key was pressed (see Keys)
-- Tanks pay a bonus, and every pit stop BOOST also repairs a heart.
game.on('enemy_death', function(s, e)
  if e.role == 'tank' then game.cash(s, 100) end
end)
game.on('pickup', function(s, item)
  if item.kind == 'boost' then game.heal(s, 1) end
end)

They arrive at the end of the tick in which they happened, in order, so a handler can safely spawn, blast or pay out. The same names work as handlers in game.effect, game.perk and game.mechanics tables. Full list: Events.

Acting on a run

Call Does
game.spawn(s, id, x, y) Spawns an enemies row, optionally at a point
game.blast(s, x, y, r, dmg) Explosion damage; returns the enemies hit and killed
game.stun(s, e, seconds) Spins out a car or stalls a unit on foot
game.remove(s, e) Takes an enemy out without a takedown
game.hurt(s, hearts) Damages the player like a police hit
game.heal, game.cash, game.nos Repairs, pays, fills or drains NOS
game.heat(s, level) Raises the wanted level
game.drop(s, kind, x, y, amount) Leaves cash on the street
game.say, game.sound Announcer lines and sounds
game.win(s, reason) Ends the run as an escape

Use these rather than editing s or requiring game modules: they are kept stable across updates, internals are not. Full list: Lua API.

Talking to other mods

Publish your own events with game.emit, named with your mod id and a dot. Any mod can listen:

-- bounty_board/main.lua
game.emit(s, 'bounty_board.claimed', {cash = 120, unit = e})

-- another mod
game.on('bounty_board.claimed', function(s, b) game.nos(s, 0.5) end)

Listening to a mod that isn't installed is fine; nothing arrives. To change another mod's rows with game.change, or to need it, name it in mod.json:

"requires": ["bounty_board"],
"after": ["sky_laser"]

requires loads your mod after those mods and skips it, with a reason on the MODS page, when one is missing or off. after only orders loading.

Keys

A key setting gives your mod a keyboard key the player can rebind on its settings page. Each press during a run sends the key event:

"settings": [
  {
    "id": "strike",
    "label": "AIRSTRIKE KEY",
    "type": "key",
    "default": "Q"
  }
]
game.on('key', function(s, id)
  if id == 'neon.strike' and game.store('charges') ~= 0 then
    game.blast(s, s.player.x, s.player.y, 80, 400)
  end
end)

Defaults can't be a key the car drives with (W, A, S, D, SPACE, LSHIFT, P) or E. Controllers have no spare buttons, so keys are keyboard only.

Saving data

game.store(key) reads and game.store(key, value) writes data your mod keeps across runs and sessions: best scores, what a player has seen, a running total. Text, numbers, true/false and tables of them, up to 64 KB per mod. It is saved when a run ends and when the game quits. For unlocks, use counters instead: the game shows their progress.

Several files

require('lib.util') loads your mod's lib/util.lua once, in your mod's environment, and returns what it returns. Other names load game or Lua modules as usual. Only list entry scripts in mod.json scripts.

When something breaks

  • Skipped mods are listed at the bottom of the MODS page with the reason: the file, row and field at fault.
  • Errors while playing switch the failing mod's code off until the next launch. Its row on the MODS page reads ERROR and shows the message.
  • mods.log, in the save folder beside mods/, has every skip reason, every error with its file and line, and your game.log lines. It starts fresh each launch.
  • RELOAD MODS on the MODS page restarts the game in place to pick up your edits.

Drawing

Three events run once per frame with a drawing kit d:

Event Draws
draw_ground On the streets, under cars and pickups
draw_world Over cars, rounds and aircraft
draw_hud Over the HUD, in 640×360 HUD space
-- A ring on every enemy near the car.
game.on('draw_world', function(s, d)
  for _, e in ipairs(game.enemies(s, s.player.x, s.player.y, 200)) do
    if d.visible(e.x, e.y, 16) then d.ring(e.x, e.y, 14, 'red', 0.6) end
  end
end)
  • Draw only. Frame rate varies by machine, so work numbers out in tick, store them in s and read them here. Run-changing calls (game.cash, game.spawn, game.random) raise an error and switch the mod off.
  • World positions, angles and animation clocks may be interpolated while drawing. The latest simulation values are restored afterward, including when drawing fails. Keep gameplay changes in tick handlers and treat interpolation records and lighting caches as engine internals.
  • Accessibility: animate with d.time; skip flashing or motion when d.reduced_flashes or d.reduced_motion is true.
  • Colours: names like 'gold' or {r, g, b}. Sprites: the game's or modid.name, optionally tinted. d.triangle draws arrows and cones.
  • HUD: d.anchor pins drawing to a screen edge across UI SCALE.
  • Budget: 2,000 kit calls per layer per frame; extra calls are dropped.
  • Enemy and weapon draw handlers get the same kit.
  • Functions: Drawing.

Mod settings

List options in mod.json settings. They appear on the mod's page in Settings > MODS, save with the player's settings, and read with game.setting(id).

"settings": [
  {
    "id": "panel",
    "label": "RUN PANEL",
    "type": "toggle",
    "default": true
  },
  {
    "id": "side",
    "label": "PANEL SIDE",
    "type": "choice",
    "values": ["LEFT", "RIGHT"],
    "default": "LEFT"
  },
  {
    "id": "opacity",
    "label": "PANEL OPACITY",
    "type": "slider",
    "min": 0.2,
    "max": 1,
    "step": 0.1,
    "default": 0.8
  }
]
  • Types: toggle, slider, choice and key (see Keys). Up to 16 per mod.
  • Read settings where you use them; players can change them mid-run from pause.
  • A saved value that no longer fits reads as the default. Values survive uninstalling the mod.
  • Fields: Mod settings.

Examples: scrapyard, dashboard, airstrike (events, a key and saved data), and the built-in Bounty Board.