openOMSI GitHub Discord

Docs / Plugins / Lua plugins

How a plugin runs

1 min readEdit this pagedocs/PLUGINS.md

The file's top level runs once when the game starts. After that the plugin reacts to events. A handler is registered with omsi.on(event, fn) (as many as you like), or by defining a global function on_<event>:

Lua
function on_frame(dt)
  -- runs ~60 times a second: keep it short, prefer omsi.every for slow work
end

omsi.on("stop_arrive", function(name, id, number, delay)
  omsi.ui.toast(string.format("%s, %s", name, omsi.fmt.delay(delay)))
end)

The events table lists them all: the game's (a crash, a ticket sold, a trip done), what changed (a door opened, the engine started, the bus arrived at a stop, a new minute, a red light run), the panels' (a click, a slider moved), messages from other plugins and from other players' games.

An event the game has to work out by comparing two frames - doors, gears, stops, the traffic light ahead - is only worked out while some plugin listens to it.

omsi.after, omsi.every and omsi.watch call a function later, regularly, or when a value changes; omsi.emit("my_event", 1, 2) sends an event to the plugin's own handlers (handy between the modules of a bigger plugin), omsi.plugin.send to another plugin.

Something wrong or missing? Fix it on GitHub or open an issue.

↑↓ to move↵ to open