openOMSI GitHub Discord
Writing plugins

Docs / Plugins / Writing plugins

Your first Lua plugin

1 min readEdit this pagedocs/PLUGINS.md

This takes about five minutes. You need openOMSI with plugins turned on (see Turn plugins on) and a text editor.

  1. Open the plugins folder next to the game. The launcher's Plugins page has a Plugins folder button. Create the folder if it is missing.

  2. Create a file hello.lua in 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)
  3. Start a map with a bus. The greeting appears, the panel shows your speed, and above 50 km/h you get a warning.

  4. While the game runs, change 50 to 30 and save the file. Within a second the game says "Lua plugin hello reloaded" and the new limit applies.

  5. Make a mistake on purpose, for example delete an end, and save. The error appears on the screen and in game.log. Fix it and save again.

Next steps:

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

↑↓ to move↵ to open