Difference between revisions of "Scripting Oolite with JavaScript"

From Elite Wiki
m (KeyPressed)
(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.)
Line 15: Line 15:
  
 
<pre>
 
<pre>
this.Name = "OXPName"
+
this.name = "OXPName"
this.Description = "A description of what the OXP does."
+
this.description = "A description of what the OXP does."
this.Version = "1.0"
+
this.version = "1.0"
  
// You can copy and paste this function and just change the "Initialise"
+
// You can copy and paste this function and just change the "initialize"
 
// to another event name to handle other OXP events (eg "STATUS_DOCKED",
 
// to another event name to handle other OXP events (eg "STATUS_DOCKED",
// "AlertConditionChanged", etc).
+
// "alertConditionChanged", etc).
 
//
 
//
this.Initialise = function () {
+
this. initialize = function () {
     Log("Initialising OXP " + Name)
+
     Log("Initialising OXP " +name)
 
}
 
}
 
</pre>
 
</pre>
Line 32: Line 32:
 
The following events are available to OXP scripts written in JavaScript.
 
The following events are available to OXP scripts written in JavaScript.
  
==== Initialise ====
+
==== initialize ====
  
The <tt>Initialise</tt> 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.
+
The <tt>initialize</tt> 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.Initialise = function () {
+
  this.initialize = function () {
 
  }
 
  }
  
  
==== KeyPressed ====
+
==== keyPressed ====
  
 
''' Broken due to merge from trunk '''
 
''' Broken due to merge from trunk '''
  
The <tt>KeyPressed</tt> 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 <tt>keyPressed</tt> 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 <tt>keycode</tt> 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.
 
The <tt>keycode</tt> 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) {
+
  this.keyPressed = function (keycode) {
 
  }
 
  }
  
==== AlertConditionChanged ====
+
==== alertConditionChanged ====
  
The <tt>AlertConditionChanged</tt> event is called when the alert condition changes. The current alert condition can be read from <tt>Player.AlertCondition</tt> and the current alert flags can be read from <tt>Player.AlertFlags</tt>.
+
The <tt>alertConditionChanged</tt> event is called when the alert condition changes. The current alert condition can be read from <tt>Player.AlertCondition</tt> and the current alert flags can be read from <tt>Player.AlertFlags</tt>.
  
  this.AlertConditionChanged = function (keycode) {
+
  this.alertConditionChanged = function (keycode) {
 
  }
 
  }
  

Revision as of 20:18, 31 March 2007

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.

this.name = "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.

initialize

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 () {
}


keyPressed

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) {
}

alertConditionChanged

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) {
}


STATUS_DOCKED

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 () {
}


STATUS_IN_FLIGHT

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 () {
}


STATUS_LAUNCHING

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

this.STATUS_LAUNCHING = function () {
}


STATUS_EXITING_WITCHSPACE

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

this.STATUS_EXITING_WITCHSPACE = function () {
}