Difference between revisions of "Scripting Oolite with JavaScript"

From Elite Wiki
Jump to: navigation, search
(See Also: Added link to new page Variables in Oolite JavaScripts)
m (changed world-scripts.plist to a link)
 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Oolite]] 1.68 and later supports scripts written in [http://en.wikipedia.org/wiki/ECMAScript ECMAScript] (more commonly known as [http://en.wikipedia.org/wiki/JavaScript JavaScript]) in addition to its traditional model based on [[property lists]]. This page provides an overview of how JavaScript is used in Oolite. The page [[Oolite JavaScript Reference: object model]] provides reference for Oolite-specific objects and methods. The page [[Oolite JavaScript Reference: world script event handlers]] provides reference for the event handlers Oolite supports. The language standards and some tutorials can be found through the Wiki links provided above.
+
[[Oolite]] 1.68 and later supports scripts written in [http://en.wikipedia.org/wiki/ECMAScript ECMAScript] (more commonly known as [http://en.wikipedia.org/wiki/JavaScript JavaScript]) in addition to its traditional model based on [[property lists]]. This page provides an overview of how JavaScript is used in Oolite. The page [[Oolite JavaScript Reference: object model]] provides reference for Oolite-specific objects and methods. The page [[Oolite JavaScript Reference: World script event handlers]] provides reference for the event handlers Oolite supports. The language standards and some tutorials can be found through the Wiki links provided above.
  
 
== Using JavaScript ==
 
== Using JavaScript ==
Line 5: Line 5:
 
Currently, JavaScript is supported for “worldScripts”, that is, as a replacement for scripts in ''script.plist'' and shipScripts, that acts as expansion for the ships AI. While a ''script.plist'' file may contain any number of separate scripts, a single JavaScript file may contain only one script.
 
Currently, JavaScript is supported for “worldScripts”, that is, as a replacement for scripts in ''script.plist'' and shipScripts, that acts as expansion for the ships AI. While a ''script.plist'' file may contain any number of separate scripts, a single JavaScript file may contain only one script.
  
If your OXP only uses one script, place a JavaScript file named ''script.js'' (or ''script.es'') in the OXP’s ''Config'' directory. If you wish to use multiple scripts, you may instead create file named ''world-scripts.plist'' in the ''Config'' directory. This [[property list]] file should consist of an array of worldScript names; the named scripts should exist in a directory named ''Scripts'' inside your OXP. As with most “atomic” files (files which cannot be merged), such script files must have a unique name to avoid conflicts with other OXPs. Using the ''world-scripts.plist'' method, you can combine JavaScript, plist and OOS scripts however you wish.
+
If your OXP only uses one script, place a JavaScript file named ''script.js'' (or ''script.es'') in the OXP’s ''Config'' directory. If you wish to use multiple scripts, you may instead create file named ''world-scripts.plist'' in the ''Config'' directory. This [[property list]] file should consist of an array of worldScript names; the named scripts should exist in a directory named ''Scripts'' inside your OXP. As with most “atomic” files (files which cannot be merged), such script files must have a unique name to avoid conflicts with other OXPs. Using the [[world-scripts.plist]] method, you can combine JavaScript, plist and OOS scripts however you wish.
  
 
Whereas plist scripts are based on polling – all scripts are run at semi-regular intervals, whether they need to be or not – scripts written in JavaScript are “event driven” – different functions, or ''event handlers'', in the script are called in response to state changes in the game, or when other events of interest happen. For instance, <code>willExitWitchSpace</code> is called just before player exits witchspace, and <code>alertConditionChanged</code> is called whenever the alert condition changes. See the [[Oolite JavaScript event handler reference|event handler reference]] for a full list of handlers and when Oolite will call them.
 
Whereas plist scripts are based on polling – all scripts are run at semi-regular intervals, whether they need to be or not – scripts written in JavaScript are “event driven” – different functions, or ''event handlers'', in the script are called in response to state changes in the game, or when other events of interest happen. For instance, <code>willExitWitchSpace</code> is called just before player exits witchspace, and <code>alertConditionChanged</code> is called whenever the alert condition changes. See the [[Oolite JavaScript event handler reference|event handler reference]] for a full list of handlers and when Oolite will call them.
 
The <code>tickle</code> handler is similar to event handlers, but is called periodically at the same times as plist scripts are run – about every 10 seconds, and when certain state changes occur. For efficiency, however, it is best to avoid using <code>tickle</code> if possible.
 
  
  
 
=== script.js File Template ===
 
=== 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. '''Every script must have a unique name.''' If multiple scripts with the same name are encountered, Oolite will arbitrary select one and discard the others.
+
Copy and paste this template into a file called script.js in the OXP Config directory. Ensure you change at least the Name value. '''Every script must have a unique name.''' If multiple scripts with the same name are encountered, Oolite will arbitrarily select one and discard the others.
  
<pre>this.name          = "My OXP Script"
+
<pre>this.name          = "My OXP Script";
this.author        = "Your Name Here"
+
this.author        = "Your Name Here";
this.copyright      = "(C) 2009 Me."
+
this.copyright      = "(C) 2013 Me.";
this.licence        = "CC-NC-by-SA 2.0"
+
this.licence        = "CC-NC-by-SA 2.0";
 
this.description    = "This OXP doesn't do very much yet.";
 
this.description    = "This OXP doesn't do very much yet.";
this.version        = "1.0 alpha 1"
+
this.version        = "1.0 alpha 1";
  
 +
"use strict";
  
 
/*  You can copy and paste this function and just change the "startUp"
 
/*  You can copy and paste this function and just change the "startUp"
     to another event name to handle other OXP events (eg "STATUS_DOCKED",
+
     to another event name to handle other OXP events (eg "shipDockedWithStation",
 
     "alertConditionChanged", etc).
 
     "alertConditionChanged", etc).
 
*/
 
*/
 
this.startUp = function()
 
this.startUp = function()
 
{
 
{
     Log("Initialising OXP " + name)
+
     log(this.name, "Initialising OXP " + this.name);
}</pre>
+
}
 +
</pre>
  
 
== See Also ==
 
== See Also ==
 +
* [https://developer.mozilla.org/en/JavaScript/Reference Mozilla JavaScript reference pages]
 
* [[Oolite JavaScript event handler reference]]
 
* [[Oolite JavaScript event handler reference]]
 
* [[Oolite JavaScript object model]]
 
* [[Oolite JavaScript object model]]
* [[JavaScript test OXP]]
+
<!-- * [[JavaScript test OXP]] -->
 
* [[Variables in Oolite JavaScripts]]
 
* [[Variables in Oolite JavaScripts]]
 
* [[Javascript Operators]]
 
* [[Javascript Operators]]
 
* [[Handling OXP Dependencies with JavaScript]]
 
* [[Handling OXP Dependencies with JavaScript]]
 +
* [[Optimization tips]]
  
 +
[[Category:Oolite]]
 
[[Category:Oolite scripting]]
 
[[Category:Oolite scripting]]

Latest revision as of 20:49, 20 June 2020

Oolite 1.68 and later supports scripts written in ECMAScript (more commonly known as JavaScript) in addition to its traditional model based on property lists. This page provides an overview of how JavaScript is used in Oolite. The page Oolite JavaScript Reference: object model provides reference for Oolite-specific objects and methods. The page Oolite JavaScript Reference: World script event handlers provides reference for the event handlers Oolite supports. The language standards and some tutorials can be found through the Wiki links provided above.

Using JavaScript

Currently, JavaScript is supported for “worldScripts”, that is, as a replacement for scripts in script.plist and shipScripts, that acts as expansion for the ships AI. While a script.plist file may contain any number of separate scripts, a single JavaScript file may contain only one script.

If your OXP only uses one script, place a JavaScript file named script.js (or script.es) in the OXP’s Config directory. If you wish to use multiple scripts, you may instead create file named world-scripts.plist in the Config directory. This property list file should consist of an array of worldScript names; the named scripts should exist in a directory named Scripts inside your OXP. As with most “atomic” files (files which cannot be merged), such script files must have a unique name to avoid conflicts with other OXPs. Using the world-scripts.plist method, you can combine JavaScript, plist and OOS scripts however you wish.

Whereas plist scripts are based on polling – all scripts are run at semi-regular intervals, whether they need to be or not – scripts written in JavaScript are “event driven” – different functions, or event handlers, in the script are called in response to state changes in the game, or when other events of interest happen. For instance, willExitWitchSpace is called just before player exits witchspace, and alertConditionChanged is called whenever the alert condition changes. See the event handler reference for a full list of handlers and when Oolite will call them.


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. Every script must have a unique name. If multiple scripts with the same name are encountered, Oolite will arbitrarily select one and discard the others.

this.name           = "My OXP Script";
this.author         = "Your Name Here";
this.copyright      = "(C) 2013 Me.";
this.licence        = "CC-NC-by-SA 2.0";
this.description    = "This OXP doesn't do very much yet.";
this.version        = "1.0 alpha 1";

"use strict";

/*  You can copy and paste this function and just change the "startUp"
    to another event name to handle other OXP events (eg "shipDockedWithStation",
    "alertConditionChanged", etc).
*/
this.startUp = function()
{
    log(this.name, "Initialising OXP " + this.name);
}

See Also