Skip to content

API Examples

Marcel1853 edited this page Oct 9, 2026 · 1 revision

🇩🇪 Deutsche Version · ⬅ For mod authors

Examples

Setting up a station by script

local stop = surface.create_entity({ name = "utl-train-stop", position = pos, direction = defines.direction.east,
  force = "player", raise_built = true })
remote.call("utl", "configure_station", stop.unit_number, { mode = "station", provide = false, request = true })
remote.call("utl", "set_request", stop.unit_number, 1, { type = "item", name = "iron-plate" }, 4000)

Blueprint with UTL settings

local mapping = stack.create_blueprint({ surface = surface, force = force, area = area })
remote.call("utl", "tag_blueprint", stack, mapping, surface)

Counting finished deliveries

local function listen()
  local ids = remote.call("utl", "get_event_ids")
  script.on_event(ids.on_delivery_completed, function(event)
    storage.done = (storage.done or 0) + 1
  end)
end
script.on_init(listen)
script.on_load(listen)

A small add-on: a shunting locomotive drives to the siding

Two own roles: a shunting depot whose trains belong to the add-on, and a siding where UTL dispatches nothing. When a shunting locomotive is free, the add-on sends it to the siding and back. What happens there (parking wagons, coupling …) would be the add-on's own logic.

control.lua of the mod shunting-example (dependency on UTLogistics):

local MOD = "shunting-example"

local function register()
  remote.call("utl", "register_role", { mod = MOD, name = "depot", caption = "Shunting depot",
    base = "depot", own_trains = true })
  remote.call("utl", "register_role", { mod = MOD, name = "siding", caption = "Siding" })
end

local function listen()
  local ids = remote.call("utl", "get_event_ids")
  -- shunting locomotive free: send it to the first siding
  script.on_event(ids.on_train_idle, function(event)
    if event.role ~= MOD .. "/depot" then return end
    local sidings = remote.call("utl", "get_stations", { addon_role = MOD .. "/siding" })
    if not sidings[1] then return end
    remote.call("utl", "send_job", event.train_id, MOD, {
      { station = sidings[1].unit, wait = { { type = "time", ticks = 30 * 60 } } },
    })
  end)
  script.on_event(ids.on_job_finished, function(event)
    if event.mod == MOD then game.print("Shunting trip " .. event.job_id .. " done") end
  end)
end

script.on_init(function() register() listen() end)
script.on_configuration_changed(register)
script.on_load(listen)

In the game: build two UTL train stops, tick "Shunting depot" on one and "Siding" on the other in the station window, and put a locomotive with the shunting depot in its schedule on the track.

More: roles and data, trips, rebuilding trains, windows.

Clone this wiki locally