openOMSI GitHub Discord
Writing plugins

Docs / Plugins / Writing plugins

The game's insides

2 min readEdit this pagedocs/PLUGINS.md

A plugin of the plugins folder can reach into the game as far as its scenery and the bus's geometry. These change only the game, so they need the game's own permissions, not full access (a plain .lua plugin has them; an .oop declares them).

What Functions Permission
every event at once omsi.on("*", function(name, ...) end) none
every scenery object omsi.scene.objects(x, y, radius), then object.var, object.set_var, object.trigger, object.set_mesh, object.set_visible on any of them world_write to change them
the bus's meshes omsi.bus.meshes(), omsi.bus.mesh_get(i) none
changing the bus's geometry omsi.bus.mesh_set(i, mesh), omsi.bus.mesh_reset(i) vehicle_write
meshes of your own in the world omsi.mesh.set(id, mesh), omsi.mesh.remove(id), omsi.mesh.clear() ui
pictures for them: files or pixels the plugin draws omsi.texture.load(id, file), omsi.texture.create(id, w, h, rgba), omsi.texture.remove(id) ui
lights in the world or on the bus omsi.light.set(id, light), omsi.light.remove(id) ui
move, turn, hide any scenery object, live omsi.object.place(id, {x, y, z, heading}), omsi.object.hide(id), omsi.object.reset(id) world_write
where a ray meets the ground omsi.scene.raycast(from, dir) none
AI cars and people where you want them omsi.traffic.spawn(at, {model, speed}), omsi.people.spawn(at, to) traffic_write
the bus's motion omsi.bus.set_velocity(v, spin), omsi.bus.push(dv) vehicle_write
driving the bus omsi.bus.hold_controls({throttle, brake, clutch, steering}, seconds), omsi.bus.release_controls() vehicle_write
workshop, dirt, another bus omsi.bus.service("refuel" / "wash" / "repair"), omsi.bus.set_dirt(x), omsi.bus.change(file, paint), omsi.game.vehicles() vehicle_write

A vehicle's or an object's script reaches only its own objects.

Lua
-- dent the bus's front a little where it hit something
omsi.on("crash", function()
  for _, m in ipairs(omsi.bus.meshes()) do
    if m.file:find("front") then
      local g = omsi.bus.mesh_get(m.index)
      for i = 2, #g.positions, 3 do            -- y of each vertex
        if g.positions[i] > 5 then g.positions[i] = g.positions[i] - 0.03 end
      end
      omsi.bus.mesh_set(m.index, { positions = g.positions })
    end
  end
end)

-- a yellow marker over the next stop
local s = omsi.duty.next_stop()
if s and s.x then
  omsi.mesh.set("marker", {
    origin = { s.x, s.y, (s.z or 0) + 4 },
    positions = { -0.5, 0, 0,  0.5, 0, 0,  0, 0, -1 },
    indices = { 0, 1, 2 },
    color = { 1, 0.8, 0 },
  })
end
Lua
-- an autopilot that keeps 30 km/h and a lamp over the bus
omsi.light.set("roof", { frame = "bus", x = 0, y = 2, z = 3.5, radius = 15, color = { 1, 0.9, 0.7 } })
function on_frame()
  local kmh = omsi.speed() or 0
  omsi.bus.hold_controls({ throttle = kmh < 30 and 0.5 or 0, brake = kmh > 33 and 0.3 or 0 })
end

-- a display you draw yourself: 64 x 32 pixels, shown on a quad over the stop
local px = {}
for i = 1, 64 * 32 do px[#px + 1] = string.char(255, 200, 0, 255) end
omsi.texture.create("display", 64, 32, table.concat(px))
omsi.mesh.set("display", { origin = { 100, 200, 3 }, texture = "display",
  positions = { 0,0,0, 2,0,0, 2,0,1, 0,0,1 }, uvs = { 0,1, 1,1, 1,0, 0,0 }, indices = { 0,1,2, 0,2,3 } })

A changed bus mesh is a copy for this bus alone; collision dents go on from it, and it lasts until bus.mesh_reset or the player leaves the bus.

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

↑↓ to move↵ to open