openOMSI GitHub Discord

Docs / Plugins / Lua plugins

The API

7 min readEdit this pagedocs/PLUGINS.md

On this page
  1. omsi.info() in detail
  2. Saved data
  3. Settings
  4. Talking to other programs
  5. Between plugins and between players
  6. Passengers' tickets

Every function is omsi.<name> - omsi.bus.state(), omsi.ui.set(...). A few rules hold for all of them:

  • Without the thing they read, readings are nil (no bus, on foot, no duty, no timetable on the map), lists are empty, and changes give false. A plugin works on every map and with every bus; a bus that lacks a script variable gives nil for it.
  • Several results come as several values: local x, y, z, heading = omsi.position(); none at all when there is nothing (if omsi.position() then).
  • Changes that can fail give true, or false and the reason: local ok, why = omsi.weather.set({ temperature = -5 }).
  • A wrong argument is an error that names the function and the argument (omsi.var: argument 1 (name) must be a string, not table); catch it with pcall where it may happen.
  • Units: metres, map coordinates x east, y north, z up; headings in degrees clockwise from north; speeds km/h; times seconds (of the day since midnight for clock times).
  • New functions come with new openOMSI versions. omsi.game.has("weather.set") tells whether this game has one; the reference says since which version each exists.

The parts of the API, with a taste of each (the reference has every function):

Lua
-- the player's bus: a dashboard in one table, and its parts by name
local s = omsi.bus.state()            -- speed, gear, rpm, doors_open, indicator, fuel, ...
omsi.bus.toggle_door(1)               -- the front door's key
omsi.bus.set_indicator("left")
local all, seated, standing = omsi.bus.passengers()
omsi.var("elec_busbar_main")          -- any script variable; omsi.trigger("bus_horn") any trigger

-- the duty and the timetable
local d = omsi.duty.get()             -- line, tour, trip, next_stop, delay, ...
for _, stop in ipairs(omsi.duty.stops()) do print(stop.name, omsi.fmt.clock(stop.departure)) end
omsi.duty.start("136", "3")           -- as the game menu takes a duty

-- the map and moving the bus
local ground = omsi.map.ground(x, y)
omsi.map.teleport(x, y, nil, 90)      -- z nil: onto the ground there
local limit = omsi.map.speed_limit()

-- the AI traffic and the people
local ahead = omsi.traffic.ahead(80)  -- the car in front, with its distance
local light = omsi.traffic.light_ahead()
omsi.traffic.set_density(50)
local waiting = omsi.people.waiting(stop_id)

-- time and weather, read and set as the game menu does
omsi.world.set_time("07:30")
omsi.weather.set({ precipitation = "snow", precipitation_rate = 0.5, temperature = -3 })
omsi.weather.preset(omsi.weather.presets()[1].file, 60)

-- camera, input, sound
local sx, sy = omsi.camera.project(x, y, z + 3)   -- where a map point is on the screen
omsi.input.hotkey("Ctrl+KeyH", function() ... end)
local id = omsi.audio.play("gong.wav", { on_bus = true, volume = 0.8 })

-- storage, files, settings, other plugins
omsi.storage.set("best_trip", 812)
omsi.files.append("trips.csv", "136,3,+30\n")
local settings = omsi.plugin.settings({ { key = "volume", type = "number", default = 50 } })
omsi.plugin.broadcast("delay", omsi.duty.delay())

-- the LAN session
for _, p in ipairs(omsi.lan.players()) do print(p.name, p.line) end
omsi.lan.send(0, "hello")             -- to this plugin on every other player's game

omsi.info() in detail#

omsi.info() is the game's state as one table (omsi.info_value(key) reads one key without building the table):

map
clock
seconds since midnight
day year view paused on_foot multiplayer
traffic
AI vehicles
speed
km/h
delay
s, late positive
map_path
the map's global.cfg
version
of openOMSI

with a bus also tile_x

tile_y
its tile, as global.cfg's [map] list numbers them
tile_pos_x
tile_pos_y
metres in that tile, x east, y north
heading
degrees, clockwise from north
vehicle_manufacturer vehicle_model
destination
the terminus the bus shows
passengers
aboard
crashes heavy_crashes

pedestrians_hit this session (as the personnel file counts them), situation, the situation file the game started from (the launcher's "continue" loads maps/<map>/laststn.osn), nil for a new game, on a duty also line

tour
trip
its number in the duty
trips
trip_name
the timetable's name of the trip
terminus
stops
how many the trip has
trip_done
true once the bus has reached the trip's last stop, the stop was skipped, or a saved game was left there: the trip is over, though the duty moves on to the next trip only a minute before it leaves
next_stop
next_stop_number
from 1
next_stop_arrival next_stop_departure
next_stop_id
the stop's object ID in the map, as the timetable names it: the same name can stand for two stops, the ID cannot
at_stop
true while the bus stands at the next stop
previous_stop
previous_stop_id
the last stop before the next one the trip calls at; none before the first
next_stop_distance
previous_stop_distance
metres in a straight line from the bus, where the stop's place is known

next_stop_distance_nav is the navigator's route-following distance in metres it is nil when navigator, the route arrows are both off (pause menu included). If Navigator are "Recalculating Route", it will holds the last value until the route is recalculated.

Saved data#

omsi.data is a table that survives the session: it is written when the game ends (and before a reload) and read back on the next start. Numbers, strings, booleans and tables of them are kept. omsi.save() writes it at once. The file is <name>.save.lua next to a one-file plugin, data.save.lua in a folder plugin's folder.

omsi.storage is the same idea as keys and values, kept as JSON in the plugin's data folder (and the same for every plugin language).

Lua
-- plugins/odometer.lua: kilometres driven, over every session
omsi.data.km = omsi.data.km or 0
function on_frame(dt)
  omsi.data.km = omsi.data.km + math.abs(omsi.var("Velocity") or 0) * dt / 3600
end
omsi.every(60, function()
  omsi.message(string.format("Odometer: %.1f km", omsi.data.km), 3)
end)

Settings#

omsi.plugin.settings({...}) declares the plugin's settings and gives back the values the player chose. The game makes a panel of them (omsi.plugin.show_settings(): checkboxes, sliders, text fields, tabs; it can be dragged and closed), keeps them in the data folder and tells the plugin each change (setting(key, value)).

Talking to other programs#

omsi.send(port, text) sends text as one UDP datagram to 127.0.0.1:port: to another program on this computer (an overlay, a dashboard, a company's tracker), never over the network. It does not wait and nothing comes back: a message sent while no program listens is lost, so keep what must not be lost in omsi.storage as well.

Returns When
true the message was handed to the system
false, reason the port is below 1024 or one of the game's multiplayer ports (27015-27024), the message is longer than 8 KB, the plugin sent 100 messages in the last second already, or the system refused it
Lua
-- plugins/live.lua: the speed and the next stop, twice a second, for a program on port 47800
omsi.every(0.5, function()
  local d = omsi.duty.get()
  omsi.send(47800, omsi.json.encode({ speed = omsi.speed(), next_stop = d and d.next_stop and d.next_stop.name }))
end)

nc -lu 47800 in a terminal shows what arrives.

Between plugins and between players#

omsi.plugin.send(to, topic, data) and omsi.plugin.broadcast(topic, data) reach other plugins in their next frame (message(from, topic, data)); omsi.plugin.list() names the plugins loaded.

In a LAN game omsi.lan.send(player, text) reaches the same plugin on another player's game (lan_message(from, text)): short texts (120 characters), about ten a second, carried in the session's own messages - a game without the plugin ignores them.

Passengers' tickets#

What a passenger asks for at the cash desk is the ticket pack's choice - unless a plugin says otherwise.

Everybody boarding the player's bus is reported once, on their way to the door, as the passenger_ticket event: who they are (id, age), what they do about a ticket (kind: "none", "stamp", "buy"), the pack's ticket and price, the stop they board at and the stop they ride to, with where both are in the duty's trip.

They wait in the doorway, and their request at the desk waits, until the plugins have had that frame (normally the next one; 5 s of game time at most), so a plugin has time to work out a fare of its own - a tariff of zones, a table of fare units, a pass - and hand it over with omsi.people.set_ticket:

Lua
omsi.on("passenger_ticket", function(p)
  if p.kind ~= "buy" or not p.destination_number then return end
  local stops = p.destination_number - p.stop_number
  omsi.people.set_ticket(p.id, {
    name = stops <= 3 and "Short hop" or "Single",
    price = stops <= 3 and 1.5 or 3,
    voice = "Zone_" .. stops,   -- Zone_<n>.wav of the passenger's voice folder, else the usual line
    tag = "my_tariff",          -- kept with the passenger: omsi.people.get(id).tag
  })
end)
  • kind changes only until the passenger is in the bus; the ticket, the name, the price and the voice until they have asked for it (omsi.people.get(id).ticket_asked).
  • ticket picks another ticket of the pack (its name, price and GivenTicket number with it); name and price alone keep the pack's number, so a bus's ticket printer and the driver's ticket key go on working.
  • A buyer always has a ticket of the pack the driver can give: ticket = 0 is refused for them, and somebody made a buyer without ticket gets the pack's ticket for their age (or, when none is for their age, the change is refused).
  • A voice line the passenger's voice folder does not have falls back to Ticket_<n>_<k>.
  • omsi.people.get(id) shows the passenger's ticket now and which plugin changed it (ticket_by). Without a plugin listening the passengers ask for what the ticket pack gives them, and nobody waits.

reduced_tickets.lua has every adult ask for the reduced fare of the stock Berlin ticket packs; passenger_log.lua writes what a plugin learns of each passenger into game.log.

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

↑↓ to move↵ to open