Repository navigation
Connecting Vehicle Scripts, the Game Script and HTML UI
In Outerra a game usually has 3 parts:
- a game script that runs the game (rules, money, jobs)
- vehicles, and every vehicle has its own script
- UI, the menus that the player sees, made with HTML
Many times you need these 3 parts to talk to each other. For example the UI wants to show something that only the vehicle knows, or the game wants to change something on the vehicle, or the player clicks a button in the UI and the game needs to know it.
On this page you will see how to connect them. As an example we will make a fuel system: the vehicle uses fuel while driving, the player can see the fuel on the screen and can buy fuel at the gas station. The code is from Transport Minigame (a small game where the player delivers cargo with trucks) and it is already working, so you can copy it, try it and change it how you want.
What is on this page:
- Section 1: how the 3 parts talk to each other
- Section 2: how the fuel system works, step by step
- Sections 3, 4 and 5: the code for each connection
- Section 6: common problems
Every part is a separate script. Here is what each one is, and what it does in the fuel system.
Every vehicle model has its own script (for example the Willys Jeep). It controls the engine, wheels, sounds, lights and the fuel tank. If there are 2 jeeps in the world, each one runs its own copy, so each one has its own fuel.
In the fuel system: the vehicle knows how big its tank is and how much fuel is in it. It uses fuel when the engine is running and stops the engine when the tank is empty.
It's basically where the game starts and runs. It keeps the money, jobs, the player’s vehicles and the zones. It also spawns the vehicles. In our example it is one main file that loads a few other files (like the databases), but you can split it into more files as you like.
In the fuel system: the game script sits between the vehicle and the UI. Every frame it asks the vehicle how much fuel is left and shows the fuel on the screen. If the fuel is low, it warns the player. When the player buys fuel, it tells the vehicle the new amount.
The menus, like the gas station, the tow service and the phone. They don't keep any game data. They ask the game script for data and tell the game script when the player clicks something.
In the fuel system: when the gas station menu opens, the menu asks the game script for the fuel and the price. When the player clicks "Fill Tank", the menu tells the game script.
The UI never talks to the vehicle directly. Everything goes through the game script.
| From | To | What | When |
|---|---|---|---|
| Vehicle | Game script | fuel left in the tank, tank size | every frame |
| Game script | UI | fuel, tank size, price | when the menu opens |
| UI | Game script | "player clicked Fill Tank" | when the button is clicked |
| Game script | Vehicle | new fuel amount | after the player pays |
These scripts can't see each other's variables. So if a script wants to share something, it exposes a function: it makes the function available, so the other script can call it.
The scripts reach each other like this:
-
Game script → vehicle: the game script spawns the vehicles itself (with
ot::js::vehicle.create, see section 3.2) . When it spawns a vehicle, it gets back an object for that vehicle and keeps this object. With this object, throughscript_interface(), it can call the functions that the vehicle exposes. -
UI → game script: when the game script opens a menu, Outerra gives the game script to the menu as a variable called
$module. So the menu can call the functions that the game script exposes, like$module.get_fuel_price().
The code for each connection is in sections 3, 4 and 5.
The fuel is saved in 2 places:
- In the vehicle script. This is the real value. The vehicle lowers the fuel while driving. The vehicle script also keeps the tank size (57 litres for the our example jeep here).
- In the game script. This is a copy. The game script keeps one copy for every vehicle the player has, so when the player switches to another vehicle, the game gives that vehicle its saved fuel back. The game script also uses this copy for the screen, the warnings and the menus.
- The vehicle lowers its fuel if the engine is running.
- The game script reads the fuel from the vehicle and updates its copy.
- The game script shows the fuel on top of the screen, like "Fuel: 41 / 57 L". At 40 % or less the text becomes orange. At 10 % or less the text becomes red and blinks between the fuel and "LOW GASOLINE!".
- When the fuel goes under 10 %, and again when it is 0, the game script sends a message to the player's phone. Each message comes only one time in a game session for tutorial purposes. If the player refuels and the fuel goes low again, there is no new message.
- The player drives into a gas station zone. The game script sees that the player is in the zone and shows a text on the screen that tells the player to press the interaction key.
- The player presses the interaction key. The game script opens the gas station menu.
- The menu asks the game script for the fuel and the price, and shows them.
- The player clicks Fill Tank. The menu tells the game script.
- The game script checks if the player is at a gas station and has enough money. If yes, it takes the money, sets its copy to a full tank, and tells the vehicle the new fuel amount, so the fuel needle on the dashboard goes up. If the player doesn’t have enough money, the game script shows “Not enough funds for fuel!” on the screen and nothing changes.
Now let’s look at the code behind the fuel system.
Outerra calls 2 setup functions in every vehicle script by itself:
-
init_chassis()runs only one time for a vehicle model, when the model is loaded. What it sets up is shared by all vehicles of this model, like wheels, sounds, lights and controls. -
init_vehicle()runs again for every vehicle that is spawned. The code is the same for all jeeps, so they all start with the same values (for example 57 litres of fuel). But every vehicle gets its own copy of these values, so later they can be different: one jeep can have 10 litres left while another one is still full.
Every vehicle needs its own fuel tank, so the fuel code goes into init_vehicle().
Inside init_vehicle(), this means the vehicle that is being created. If you put a function on this, the game script can call it:
function init_vehicle() {
// ...
// Tank size and fuel in the tank, in litres. The vehicle starts with a full tank.
this.fuel_capacity = 57.0;
this.remaining_fuel = this.fuel_capacity;
this.last_fuel_pos = null; // last position, used to burn more fuel when going uphill
// The game script calls this function to read the tank size
this.get_fuel_capacity = function () {
return this.fuel_capacity;
};
// The game script calls this function to read the fuel
this.get_remaining_fuel = function () {
return this.remaining_fuel;
};
// The game script calls this function to change the fuel (for example when refueling)
this.set_remaining_fuel = function (amount) {
this.remaining_fuel = amount;
};
}The jeep script also uses this.remaining_fuel in 2 other places:
-
simulation_step(dt)runs on every physics step. When the engine is running, it lowers the fuel. More throttle, higher RPM and going uphill use more fuel. When the fuel is 0, the engine stops and the player sees "Out of fuel!". The jeep uses this formula:let engine_load_total = throttle * weight_multiplier * slope_multiplier * rpm_ratio; let fuel_drain = engine_load_total * jeep_base_rate * dt; // jeep_base_rate = 0.04 this.remaining_fuel -= fuel_drain;
-
throttle: how much the player presses the gas, from 0 to 1 -
rpm_ratio: engine RPM divided by the max RPM (at least 0.1) -
weight_multiplier:1 + (mass / 1100) * 0.15. The jeep's mass is always 1100, so this is always 1.15 -
slope_multiplier: 1 on a flat road, up to 3 when going uphill, down to 0.1 when going downhill -
dt: the time since the last physics step, in seconds
-
-
update_frame(dt)runs every frame and moves the fuel needle on the dashboard, usingthis.remaining_fuel / this.fuel_capacity.
ot::js::vehicle.create spawns a new vehicle in the world and gives back an object for that vehicle. The game script keeps this object and uses it later to control the vehicle (get in, move it, get its position and so on).
To call a function from the vehicle's init_vehicle(), like get_remaining_fuel(), first call script_interface() on that object:
// Spawn a vehicle and keep its object
let vehicle = engine_module.$query_interface('ot::js::vehicle.create', path, pos, rot);
// Later: call the functions from the vehicle script's init_vehicle()
let script_api = vehicle.script_interface();
if (script_api && typeof script_api.get_remaining_fuel === 'function') {
let litres = script_api.get_remaining_fuel();
}Note: what is
engine_module? It is the game script itself. Insideon_initialize(),thismeans the game script, but in other functions likenew_game()orvisual_update(),thisis something else. So at the start ofon_initialize()the game script saves itself in a variable:engine_module = this;. After that, every function can useengine_moduleto reach the game script. You will see it in many examples.
In Transport Minigame all spawned vehicles are kept in spawned_vehicles, and the vehicle that the player drives is current_vehicle.
The typeof ... === 'function' check is there because not every vehicle has the same functions. A vehicle from another mod maybe doesn't have get_remaining_fuel. Without this check the script gives an error.
The engine calls on_initialize() one time when the game starts. Here this means the game script itself. If you put a function on this here, every HTML menu can call it.
function on_initialize() {
// Save the game script as engine_module (see the note in section 3.2)
engine_module = this;
// Only gives back a value
this.get_fuel_price = function () {
return fuel_price_per_liter;
};
// Changes something and says if it worked
this.refuel_vehicle = function () {
// ... checks if the player is at a gas station and has enough money ...
return true; // or false
};
return true;
}Some simple rules we use for these functions:
- Functions that start with
get_,is_orcheck_only give back information, they don't change anything. - Functions that do something (buy, refuel, accept a job) give back a result, so the menu knows if it worked. This is
true/falseor a short text like"SUCCESS","INSUFFICIENT_FUNDS"or"NO_PARKING".
The game script opens a menu with open_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);Test