Docs / Plugins / Writing plugins
Your first Lua plugin
This takes about five minutes. You need openOMSI with plugins turned on (see Turn plugins on) and a text editor.
-
Open the
pluginsfolder next to the game. The launcher's Plugins page has a Plugins folder button. Create the folder if it is missing. -
Create a file
hello.luain it and paste this:Lua -- plugins/hello.lua -- Greet the driver when they get into a bus. omsi.on("vehicle", function(name) if name then omsi.message("Good morning! Today you drive the " .. name, 6) end end) -- A small panel with the speed, top left. local function show() if not omsi.has_vehicle() then omsi.ui.remove("speed") return end omsi.ui.set("speed", { anchor = "top_left", x = 16, y = 60, width = 200, children = { { type = "text", text = "Speed", size = 12 }, { type = "text", text = omsi.fmt.speed(omsi.speed()), size = 24, weight = "bold" }, }, }) end omsi.every(0.5, show) -- A warning above 50 km/h, checked once a second. omsi.every(1, function() local kmh = omsi.var("Velocity") if kmh and kmh > 50 then omsi.message(string.format("Slow down: %.0f km/h", kmh), 1) end end) -
Start a map with a bus. The greeting appears, the panel shows your speed, and above 50 km/h you get a warning.
-
While the game runs, change
50to30and save the file. Within a second the game says "Lua plugin hello reloaded" and the new limit applies. -
Make a mistake on purpose, for example delete an
end, and save. The error appears on the screen and ingame.log. Fix it and save again.
Next steps:
- Read How a plugin runs for events, timers and reloading.
- Look at the example plugins.
- Browse the API reference for what else you can read and change.
Something wrong or missing? Fix it on GitHub or open an issue.