Skip to content

Connecting Vehicle Scripts, the Game Script and HTML UI

Batuhan Fatih Dilli edited this page Nov 3, 2026 · 30 revisions

This page explains how the three script layers of Transport Minigame talk to each other, using the fuel system as a worked example. Read it if you want to:

  • understand how Outerra engine connects script layers,
  • make your own vehicle work with the fuel system,
  • add a new HTML window that reads or changes game state,
  • understand where a value shown in the UI actually comes from.

1. Layer 1: Vehicle script → Game script

1.1 Exposing a function from a vehicle

Vehicle scripts have two init hooks. Pick the right one:

Hook | Runs | Use it for

  • init_chassis(param) | Once per model (shared by every copy of the vehicle) | Wheels, sounds, lights, input axes

  • init_vehicle() | Once per instance (each spawned vehicle) | Per-vehicle state and functions you want to expose

Fuel is per vehicle (two jeeps have two tanks), so it lives in init_vehicle():

function init_vehicle() {
    // ...
    this.remaining_fuel = 57.0;   // litres, should match fuel_capacity in the DB
    this.last_fuel_pos = null;

    this.get_remaining_fuel = function () {
        return this.remaining_fuel;
    };

    this.set_remaining_fuel = function (amount) {
        this.remaining_fuel = amount;
    };
}

The vehicle keeps using this.remaining_fuel internally:

simulation_step(dt) drains it while the engine runs (throttle × RPM × slope × weight × 0.04 × dt) and stalls the engine at 0 ("Out of fuel!"). update_frame(dt) moves the dashboard fuel needle toward remaining_fuel.

1.2 Calling it from the game script

The game script gets the vehicle object from ot::js::vehicle.create, then calls script_interface() to reach the functions defined in init_vehicle():

let vehicle = engine_module.$query_interface('ot::js::vehicle.create', path, pos, rot);

let script_api = vehicle.script_interface();
if (script_api && typeof script_api.get_remaining_fuel === 'function') {
    let litres = script_api.get_remaining_fuel();
}

Always check typeof ... === 'function' first. Not every vehicle script exposes the same functions (mod vehicles especially). The check lets the game skip a missing function instead of crashing.

2. Layer 2: Game script → HTML

2.1 Exposing a function to HTML

Everything assigned to this inside on_initialize() becomes callable from HTML as $module.():

function on_initialize() {
    engine_module = this;   // keep a reference for use outside on_initialize

    // Read-only getter
    this.get_fuel_price = function () {
        return fuel_price_per_liter;
    };

    // Action: changes state, returns a result HTML can react to
    this.refuel_vehicle = function () {
        // ... checks zone and money, updates state ...
        return true;   // or false
    };

    // Expose an existing top-level function as-is
    this.get_carrier_data = get_carrier_data;

    return true;
}

Conventions used in this mod:

  • Getters are named get_, is_ or check_* and return plain data (numbers, strings, arrays, objects).
  • Actions return something HTML can branch on: true/false, or a status string such as "SUCCESS", "INSUFFICIENT_FUNDS", "NO_PARKING".
  • One-shot flags such as check_jobs_update() and check_just_completed() return their value once, then reset. Call them once per check, not in a loop that ignores the result.

2.2 Opening an HTML window

let ui_params = `?width=${screen_size.x}&height=${screen_size.y}&transparent=true&frame=false&alpha=true`;
engine_module.open_window("www/gas_station.html" + ui_params);

You can append your own parameters to pass one-time data when the window opens. The tow service does this:

engine_module.open_window("www/tow_service.html" + ui_params +
    `&base_cost=${base_cost}&fuel_cost=${fuel_cost}&out_of_fuel=${is_out_of_fuel}`);

Use URL parameters for values computed at open time. Use $module.get_*() for values that can change while the window is open.

2.3 Calling HTML from the game script

The game script can also run a JS function inside the UI:

engine_module.ui.exec("hide_interaction_prompt();");

3. Layer 3: HTML → Game script

3.1 Using $module

Declare $module at the top of your script. The engine fills it in when the window opens:

<script>
    var $module;

    window.onload = function () {
        refreshUI();
    };

    function refreshUI() {
        if (typeof $module === 'undefined') return;   // opened outside the game

        let fuel = $module.get_vehicle_fuel();        // { current, max }
        let price = $module.get_fuel_price();

        document.getElementById("fuel-text").innerText =
            `${Math.floor(fuel.current)} / ${fuel.max} L`;
    }

    function requestRefuel() {
        if ($module.refuel_vehicle()) refreshUI();
    }
</script>

Calls are synchronous: the return value is available immediately.

Always call methods on $module ($module.get_active_carrier_id(true)). Do not copy them into a variable first (let f = $module.get_active_carrier_id; f(true);). Some functions use this internally and break when detached.

Test

Clone this wiki locally