Repository navigation
How to make custom keybinding
In Outerra, keys are not written directly in the script. Instead there are 2 separate parts:
- a config file that gives an action a name and a key, for example "the action
open_menuis the key P" - a script (a game script or a vehicle script) that says what happens when that action is used, for example "open the menu"
This way the key and the code stay separate. The script only knows the action name, not the key. One action can also have more than one key, or a joystick button or axis.
On this page you will see how to add your own keys to a mod.
What is on this page:
- Section 1: how it works
- Section 2: the config file
- Section 3: events, keys that do something once
- Section 4: values, keys that change a value
- Section 5: common problems
- In your mod you create a config file with your actions. Every action has a name and one or more keys.
- In a script you register a function for that action name.
- When the player uses the key, Outerra calls your function.
The full name of an action has 3 parts:
ot / player / open_menu
| | |
| | action name
| group name
config file name (ot.cfg)
You use this full name in the script. If one part is different, the function is never called.
There are 2 kinds of actions:
- Events: something happens one time when the key is pressed, for example "open the menu".
- Values: the action has a number, by default between -1 and 1. While the key is held, the number changes, for example the throttle of a vehicle goes up. If the action is on a joystick axis, the number is the axis position.
The script decides which kind it uses.
A new mod doesn't have these folders and files, you need to create them yourself. Inside your mod, create a defaults folder, inside it an iomap folder, and inside that your config file:
game/mods/your_mod/
└── defaults/
└── iomap/
└── ot.cfg
The file name is the first part of the action name. Outerra has one file for every area, for example ot.cfg for the player and general keys, vehicle.cfg for vehicles and air.cfg for aircraft. If you put your keys in ot.cfg, your actions start with ot/.
You don't need to copy Outerra's default file. Your file only has your new actions, Outerra adds them to its own.
The file is in JSON format. For example:
{
"version": 1,
"groups": [
{
"name": "player",
"actions": [
{
"name": "open_menu",
"bindings": [
{ "event_name": "P" }
]
},
{
"name": "show_info",
"bindings": [
{ "event_name": "Z", "modifiers": "Ctrl" }
]
},
{
"name": "reset_position",
"bindings": [
{ "event_name": "X", "modifiers": "Alt Shift" }
]
}
]
}
]
}| Field | What it is |
|---|---|
version |
version of the file format, it is 1
|
groups |
list of groups. A group is the second part of the action name |
name (in a group) |
group name, here player
|
actions |
list of actions in this group |
name (in an action) |
action name, the last part of the full name |
bindings |
the keys for this action, there can be more than one |
event_name |
the key, for example "P"
|
modifiers |
optional, keys that must be held together. One key like "Ctrl", or more with a space, like "Alt Shift"
|
mode |
optional, only for values: what the key does with the value |
For the names of other keys, like
"Space"or"F1", look at Outerra's defaultiomapfiles.
So in this example:
-
open_menubecomesot/player/open_menu, key P -
show_infobecomesot/player/show_info, key Ctrl + Z -
reset_positionbecomesot/player/reset_position, key Alt + Shift + X
For values, every key can have a mode that says what the key does with the value:
| Mode | What the key does |
|---|---|
+ (or empty) |
moves the value to +1 |
- |
moves the value to -1 |
0 |
moves the value back to 0 |
toggle |
switches between min and max |
toggle+ |
switches between 0 and max |
toggle- |
switches between min and 0 |
For example Outerra's ot/player/move has W with mode + (forward) and S with mode - (backward):
{
"name": "move",
"bindings": [
{ "event_name": "W", "mode": "+" },
{ "event_name": "S", "mode": "-" }
]
}The mode only sets where the value should go. How fast it gets there, and the min and max values, are set in the script.
In the game script, in on_initialize(), call this.register_event() with the full action name and a function:
function on_initialize() {
engine_module = this;
// P: open a menu
this.register_event("ot/player/open_menu", function () {
engine_module.open_window("www/menu.html");
});
// Ctrl + Z: show a text on the screen
this.register_event("ot/player/show_info", function () {
engine_module.fade("Hello!");
});
return true;
}When the player presses the key, Outerra calls the function. Inside the function, use engine_module to reach the game script, like in other functions outside on_initialize().
A vehicle script can register events the same way. In a vehicle script, put them in init_chassis(). This function runs one time for the vehicle model, so the keys are registered one time and work for every vehicle of that model.
You don't have to add every action to your config file. You can also register a function for an action that is already in Outerra's default file, for example:
// Left mouse button
this.register_event("ot/player/fire", function () {
// ...
});
// V
this.register_event("ot/player/toggle_seat", function () {
// ...
});You can find all existing actions and their keys in Outerra's default iomap files.
Vehicles mostly use values, for example for the throttle or the steering.
// In the vehicle script, inside init_chassis()
// Throttle: the value goes up while the key is held
this.register_axis("vehicle/controls/power", { vel: 0.5 }, throttle_action);
// Steering: the value goes from -1 to 1, and back to the center when the key is released
this.register_axis("vehicle/controls/steering", { vel: -0.8, acc: 50, center: -0.5 }, steering_action);
function throttle_action(v, dt) {
// this refers to the vehicle instance, v is the current value of the action
this.engine_load = v;
}The function gets these values:
| Parameter | What it is |
|---|---|
v |
the value of the action |
dt |
time since the last frame, in seconds |
ch |
channel number, see below |
Channels: some actions can control more than one thing. For example, a plane with 2 engines can have one action to start the engines. The player can bind one key to engine 1, another key to engine 2, and a third key to both. When your function is called, ch tells you which one: 1 for engine 1, 2 for engine 2, and 0 for all of them. If your action controls only one thing, you can ignore ch.
The second argument of register_axis says how the value changes when it is controlled with keys:
| Option | What it does |
|---|---|
vel |
how fast the value changes per second while the key is held |
acc |
how fast it reaches that speed after the key is pressed |
center |
how fast the value goes back to 0 after the key is released. 0 means it stays where it is |
minval |
lowest value, should be -1 or more (to work with joysticks) |
maxval |
highest value, should be 1 or less |
steps |
for keys that change the value in steps, how many steps there are between 0 and 1 (for example flaps) |
channels |
how many extra channels the action supports, max 7 |
Negative vel or center: the minus doesn't mean the other direction. It means the speed depends on how fast the vehicle drives: when the vehicle stands, vel: -0.8 works like 0.8, and the faster the vehicle drives, the slower the value changes. This is useful for steering: at high speed the wheels turn slower, so the vehicle doesn't turn too sharply.
There is also register_switch(action, function), a short way to make an action that switches between 2 values, like lights on and off.
| Problem | Reason |
|---|---|
| Pressing the key does nothing | The full name in the script is not the same as in the config file. Check all 3 parts: file name, group name and action name |
| Pressing the key does nothing | The config file is not in defaults/iomap of your mod |
| None of your keys work | The JSON has an error, for example a missing comma or bracket |
| The key also does something else | Outerra already uses this key for another action. Check Outerra's default files before you choose a key. Sharing a key is ok if the other action is not used at the same time, for example G is also ot/player/throw. If both actions are needed at the same time, choose a free key |
Test