Scripting Oolite with JavaScript

From Elite Wiki
Revision as of 21:18, 31 March 2007 by Ahruman (talk | contribs) (Changed properties/instance variables to consistently start with lowercase, which is more consistent with normal JS usage, PList script usage and ObjC usage. Also changed initialise to initialize.)
Jump to: navigation, search

JavaScript file format

To use JavaScript for an OXP, place a file script.js in the OXP's Config directory. Do not also include a script.plist or script.oos file.

OXP scripts written in JavaScript are "event driven" - different functions of the script are called in response to state changes in the game, or when other events of interest happen.

Sometimes the event function will be called periodically, such as STATUS_DOCKED and STATUS_IN_FLIGHT. These functions will be called about every 10 seconds as long as the game is in that state.

Other event functions will only be called when that state becomes true, like KeyPressed and STATUS_EXITING_WITCHSPACE.

script.js File Template

Copy and paste this template into a file called script.js in the OXP Config directory. Ensure you change at least the Name value. = "OXPName"
this.description = "A description of what the OXP does."
this.version = "1.0"

// You can copy and paste this function and just change the "initialize"
// to another event name to handle other OXP events (eg "STATUS_DOCKED",
// "alertConditionChanged", etc).
this. initialize = function () {
    Log("Initialising OXP " +name)

Scripting Events

The following events are available to OXP scripts written in JavaScript.


The initialize event is called after all OXPs have been loaded. It can be used to do once-off initialisation such as registering to listen for certain keystrokes etc.

this.initialize = function () {


Broken due to merge from trunk

The keyPressed event is called when a key the OXP is listening for has been pressed. It will be called once for each time the key is pressed, and will not be called again until the key is released and then pressed again.

The keycode argument gives the keycode of the key that was pressed. This can be used to determine which key was pressed if the OXP is listening for more than one key.

this.keyPressed = function (keycode) {


The alertConditionChanged event is called when the alert condition changes. The current alert condition can be read from Player.AlertCondition and the current alert flags can be read from Player.AlertFlags.

this.alertConditionChanged = function (keycode) {


The STATUS_DOCKED event is called periodically while the player is docked at a station or other entity with a docking port.

this.STATUS_DOCKED = function () {


The STATUS_IN_FLIGHT event is called periodically while the player is flying in normal space or interstellar space (due to a misjump).

this.STATUS_IN_FLIGHT = function () {


The STATUS_LAUNCHING event is called once when the player has launched from a dock.

this.STATUS_LAUNCHING = function () {


The STATUS_EXITING_WITCHSPACE event is called once when the player arrives in a new system.

this.STATUS_EXITING_WITCHSPACE = function () {