Difference between revisions of "Scripting Oolite with JavaScript"
Cholmondely (talk | contribs) (→See Also: Added link to a_c on problems with updating Spidermonkey) |
|||
(27 intermediate revisions by 15 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 |
+ | [[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 == |
||
− | Currently, JavaScript is |
+ | 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 |
+ | 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 – |
+ | 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. |
− | Certain special “tickle” handlers, such as <code>STATUS_DOCKED</code> and <code>STATUS_IN_FLIGHT</code>, will be called periodically. These functions will be called about every 10 seconds as long as the game is in the relevant state. For efficiency, however, it is best to avoid these events if possible. |
||
+ | See the [[Oolite JavaScript event handler reference|event handler reference]] for a full list of handlers and when Oolite will call them. |
||
=== 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 |
+ | 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. |
+ | |||
+ | Note: If you do not want to react to one of these events anyway you can fully remove that function. This list is complete now as it is easier for novice users to remove than to add. |
||
<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) |
+ | this.copyright = "(C) 2021 Me."; |
+ | 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"; |
||
+ | |||
+ | // Game State |
||
+ | |||
+ | this.gamePaused = function() |
||
+ | { |
||
+ | log(this.name, "gamePaused()"); |
||
+ | } |
||
+ | |||
+ | this.gameResumed = function() |
||
+ | { |
||
+ | log(this.name, "gameResumed()"); |
||
+ | } |
||
+ | |||
+ | this.playerWillSaveGame = function() |
||
+ | { |
||
+ | log(this.name, "playerWillSaveGame()"); |
||
+ | } |
||
− | /* You can copy and paste this function and just change the "startUp" |
||
− | to another event name to handle other OXP events (eg "STATUS_DOCKED", |
||
− | "alertConditionChanged", etc). |
||
− | */ |
||
this.startUp = function() |
this.startUp = function() |
||
{ |
{ |
||
− | Log("Initialising OXP " + name); |
||
+ | log(this.name, "startup()"); |
||
− | }</pre> |
||
+ | } |
||
− | === Scripting Events === |
||
+ | this.startUpComplete = function() |
||
+ | { |
||
+ | log(this.name, "startupComplete()"); |
||
+ | } |
||
− | The following events are available to OXP scripts written in JavaScript. |
||
+ | // Docking |
||
− | ==== <code>startUp</code> ==== |
||
+ | this.shipWillDockWithStation = function(station) |
||
+ | { |
||
+ | log(this.name, "shipWillDockWithStation("+station+")"); |
||
+ | } |
||
− | The <code>startUp</code> handler 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.shipDockedWithStation = function(station) |
||
+ | { |
||
+ | log(this.name, "shipDockedWithStation("+station+")"); |
||
+ | } |
||
− | + | this.shipWillLaunchFromStation = function(stationLaunchedFrom) |
|
− | + | { |
|
− | + | log(this.name, "shipWillLaunchFromStation("+stationLaunchedFrom+")"); |
|
− | + | } |
|
− | ==== <code>reset</code> ==== |
||
+ | this.shipLaunchedFromStation = function(stationLaunchedFrom) |
||
+ | { |
||
+ | log(this.name, "shipLaunchedFromStation("+stationLaunchedFrom+")"); |
||
+ | } |
||
− | The <code>reset</code> handler is called whenever the player is respawned, such as after dying or when loading a saved game. It should be used to reset any local state in the script. |
||
+ | this.playerStartedAutoPilot = function() |
||
+ | { |
||
+ | log(this.name, "playerStartedAutoPilot()"); |
||
+ | } |
||
− | + | this.playerCancelledAutoPilot = function() |
|
− | + | { |
|
− | + | log(this.name, "playerCancelledAutoPilot()"); |
|
− | + | } |
|
+ | this.playerDockingClearanceCancelled = function() |
||
+ | { |
||
+ | log(this.name, "playerDockingClearanceCancelled()"); |
||
+ | } |
||
− | <!-- |
||
+ | this.playerDockingClearanceExpired = function() |
||
− | ==== keyPressed ==== |
||
+ | { |
||
+ | log(this.name, "playerDockingClearanceExpired()"); |
||
+ | } |
||
− | ''' Broken due to merge from trunk ''' |
||
+ | this.playerDockingClearanceGranted = function() |
||
+ | { |
||
+ | log(this.name, "playerDockingClearanceGranted()"); |
||
+ | } |
||
− | 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. |
||
+ | this.playerDockingRefused = function() |
||
+ | { |
||
+ | log(this.name, "playerDockingRefused()"); |
||
+ | } |
||
− | 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.playerRequestedDockingClearance = function(message) |
||
+ | { |
||
+ | log(this.name, "playerRequestedDockingClearance("+message+")"); |
||
+ | } |
||
− | + | this.playerRescuedEscapePod = function(fee, reason, occupant) |
|
− | + | { |
|
+ | log(this.name, "playerRescuedEscapePod("+fee + ", " + reason + ", " + occupant+")"); |
||
+ | } |
||
− | ==== alertConditionChanged ==== |
||
+ | this.playerCompletedContract = function(type, result, fee, contract) |
||
+ | { |
||
+ | log(this.name, "playerCompletedContract("+fee + ", " + reason + ", " + occupant+")"); |
||
+ | } |
||
− | The <code>alertConditionChanged</code> handler is called when the alert condition changes. The current alert condition can be read from <code>olayer.alertCondition</code> and the current alert flags can be read from <code>player.alertFlags</code>. |
||
+ | this.playerEnteredContract = function(type, contract) |
||
+ | { |
||
+ | log(this.name, "playerEnteredContract("+type + ", " + contract + ")"); |
||
+ | } |
||
− | this.alertConditionChanged = function () |
||
+ | // Witchspace jumps |
||
− | { |
||
− | // Your code here |
||
− | } |
||
+ | this.playerStartedJumpCountdown = function(type, seconds) |
||
+ | { |
||
+ | log(this.name, "playerStartedJumpCountdown("+type + ", " + seconds + ")"); |
||
+ | } |
||
− | ==== STATUS_DOCKED ==== |
||
+ | this.playerCancelledJumpCountdown = function() |
||
+ | { |
||
+ | log(this.name, "playerCancelledJumpCountdown()"); |
||
+ | } |
||
− | The <tt>STATUS_DOCKED</tt> event is called periodically while the player is docked at a station or other entity with a docking port. |
||
+ | this.playerJumpFailed = function(reason) |
||
+ | { |
||
+ | log(this.name, "playerJumpFailed(" + reason + ")" ); |
||
+ | } |
||
− | + | this.shipWillEnterWitchspace = function(cause, destination) |
|
− | + | { |
|
+ | log(this.name, "shipWillEnterWitchspace(" + cause + ", " + destination + ")"); |
||
+ | } |
||
+ | this.shipWillExitWitchspace = function() |
||
+ | { |
||
+ | log(this.name, "shipWillEnterWitchspace()"); |
||
+ | } |
||
− | ==== STATUS_IN_FLIGHT ==== |
||
+ | this.shipExitedWitchspace = function() |
||
+ | { |
||
+ | log(this.name, "shipExitedWitchspace()"); |
||
+ | } |
||
− | The <tt>STATUS_IN_FLIGHT</tt> event is called periodically while the player is flying in normal space or interstellar space (due to a misjump). |
||
+ | this.playerEnteredNewGalaxy = function(galaxyNumber) |
||
+ | { |
||
+ | log(this.name, "playerEnteredNewGalaxy(" + galaxyNumber + ")"); |
||
+ | } |
||
− | this.STATUS_IN_FLIGHT = function () { |
||
+ | // Enter/Exit Aegis |
||
− | } |
||
+ | this.shipEnteredStationAegis = function(station) |
||
+ | { |
||
+ | log(this.name, "shipEnteredStationAegis(" + station + ")"); |
||
+ | } |
||
+ | this.shipExitedStationAegis = function(station) |
||
+ | { |
||
+ | log(this.name, "shipExitedStationAegis(" + station + ")"); |
||
+ | } |
||
− | ==== STATUS_LAUNCHING ==== |
||
+ | this.shipEnteredPlanetaryVicinity = function(planet) |
||
+ | { |
||
+ | log(this.name, "shipEnteredPlanetaryVicinity(" + planet + ")"); |
||
+ | } |
||
− | The <tt>STATUS_LAUNCHING</tt> event is called once when the player has launched from a dock. |
||
+ | this.shipExitedPlanetaryVicinity = function(planet) |
||
+ | { |
||
+ | log(this.name, "shipExitedPlanetaryVicinity(" + planet + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipApproachingPlanetSurface = function(planet) |
||
+ | { |
||
+ | log(this.name, "shipApproachingPlanetSurface(" + planet + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipLeavingPlanetSurface = function(planet) |
||
+ | { |
||
+ | log(this.name, "shipLeavingPlanetSurface(" + planet + ")"); |
||
+ | } |
||
+ | |||
+ | // Combat |
||
+ | |||
+ | this.alertConditionChanged = function(newCondition, oldCondition) |
||
+ | { |
||
+ | log(this.name, "alertConditionChanged(" + newCondition + ", " + oldCondition + ")"); |
||
+ | } |
||
+ | |||
+ | this.playerTargetedMissile = function(missile) |
||
+ | { |
||
+ | log(this.name, "playerTargetedMissile(" + missile + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipAttackedOther = function(other) |
||
+ | { |
||
+ | log(this.name, "shipAttackedOther(" + other + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipAttackedWithMissile = function(missile, whom) |
||
+ | { |
||
+ | log(this.name, "shipAttackedWithMissile(" + missile + ", " + whom + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipBeingAttacked = function(whom) |
||
+ | { |
||
+ | log(this.name, "shipBeingAttacked(" + whom + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipBeingAttackedByCloaked = function() |
||
+ | { |
||
+ | log(this.name, "shipBeingAttackedByCloaked()"); |
||
+ | } |
||
+ | |||
+ | this.shipKilledOther = function(whom, damageType) |
||
+ | { |
||
+ | log(this.name, "shipKilledOther(" + whom + ", " + damageType + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipReleasedEquipment = function(mine) |
||
+ | { |
||
+ | log(this.name, "shipReleasedEquipment(" + mine + ")"); |
||
+ | } |
||
+ | |||
+ | |||
+ | this.shipTargetDestroyed = function(target) |
||
+ | { |
||
+ | log(this.name, "shipTargetDestroyed(" + target + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipDied = function(whom, why) |
||
+ | { |
||
+ | log(this.name, "shipDied(" + whom + ", " + why + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipFiredMissile = function(missile, target) |
||
+ | { |
||
+ | log(this.name, "shipFiredMissile(" + missile + ", " + target + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipTargetLost = function(target) |
||
+ | { |
||
+ | log(this.name, "shipTargetLost(" + target + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipTargetCloaked = function() |
||
+ | { |
||
+ | log(this.name, "shipTargetCloaked()"); |
||
+ | } |
||
+ | |||
+ | this.weaponsSystemsToggled = function(state) |
||
+ | { |
||
+ | log(this.name, "weaponsSystemsToggled(" + state + ")"); |
||
+ | } |
||
+ | |||
+ | // Equipment and Cargo |
||
+ | |||
+ | this.equipmentAdded = function(equipmentKey) |
||
+ | { |
||
+ | log(this.name, "equipmentAdded(" + equipmentKey + ")"); |
||
+ | } |
||
+ | |||
+ | this.equipmentDamaged = function(equipment) |
||
+ | { |
||
+ | log(this.name, "equipmentDamaged(" + equipment + ")"); |
||
+ | } |
||
+ | |||
+ | this.equipmentRemoved = function(equipmentKey) |
||
+ | { |
||
+ | log(this.name, "equipmentRemoved(" + equipmentKey + ")"); |
||
+ | } |
||
+ | |||
+ | this.equipmentRepaired = function(equipment) |
||
+ | { |
||
+ | log(this.name, "equipmentRepaired(" + equipment + ")"); |
||
+ | } |
||
+ | |||
+ | this.playerBoughtCargo = function(commodity, units, price) |
||
+ | { |
||
+ | log(this.name, "playerBoughtCargo(" + commodity + ", " + units + ", " + price + ")"); |
||
+ | } |
||
+ | |||
+ | this.playerBoughtEquipment = function(equipment, paid) |
||
+ | { |
||
+ | log(this.name, "playerBoughtEquipment(" + equipment + ", " + paid + ")"); |
||
+ | } |
||
+ | |||
+ | this.playerBoughtNewShip = function(ship, price) |
||
+ | { |
||
+ | log(this.name, "playerBoughtNewShip(" + ship + ", " + price + ")"); |
||
+ | } |
||
+ | |||
+ | this.playerChangedPrimedEquipment = function(equipmentKey) |
||
+ | { |
||
+ | log(this.name, "playerChangedPrimedEquipment(" + equipmentKey + ")"); |
||
+ | } |
||
+ | |||
+ | this.playerReplacedShip = function(ship) |
||
+ | { |
||
+ | log(this.name, "playerReplacedShip(" + ship + ")"); |
||
+ | } |
||
+ | |||
+ | this.playerSoldCargo = function(commodity, units, price) |
||
+ | { |
||
+ | log(this.name, "playerSoldCargo(" + commodity + ", " + units + ", " + price + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipScoopedFuel = function() |
||
+ | { |
||
+ | log(this.name, "shipScoopedFuel()"); |
||
+ | } |
||
+ | |||
+ | this.shipScoopedOther = function(whom) |
||
+ | { |
||
+ | log(this.name, "shipScoopedOther(" + whom + ")"); |
||
+ | } |
||
+ | |||
+ | this.playerWillBuyNewShip = function(dataKey, shipyard, price, tradeIn) |
||
+ | { |
||
+ | log(this.name, "playerWillBuyNewShip(" + dataKey + ", " + shipyard + ", " + price + ", " + tradeIn + ")"); |
||
+ | } |
||
+ | |||
+ | this.playerWillReplaceShip = function(dataKey) |
||
+ | { |
||
+ | log(this.name, "playerWillReplaceShip(" + dataKey + ")"); |
||
+ | } |
||
+ | |||
+ | // Other |
||
+ | |||
+ | this.chartHightlightModeChanged = function(newMode) |
||
+ | { |
||
+ | log(this.name, "chartHightlightModeChanged(" + newMode + ")"); |
||
+ | } |
||
+ | |||
+ | this.compassTargetChanged = function(whom, mode) |
||
+ | { |
||
+ | log(this.name, "compassTargetChanged(" + whom + ", " + mode + ")"); |
||
+ | } |
||
+ | |||
+ | this.dayChanged = function(newday) |
||
+ | { |
||
+ | log(this.name, "dayChanged(" + newday + ")"); |
||
+ | } |
||
+ | |||
+ | this.escapePodSequenceOver = function() |
||
+ | { |
||
+ | log(this.name, "escapePodSequenceOver()"); |
||
+ | } |
||
+ | |||
+ | this.guiScreenChanged = function(to, from) |
||
+ | { |
||
+ | log(this.name, "guiScreenChanged(" + to + ", " + from + ")"); |
||
+ | } |
||
+ | |||
+ | this.guiScreenWillChange = function(to, from) |
||
+ | { |
||
+ | log(this.name, "guiScreenWillChange(" + to + ", " + from + ")"); |
||
+ | } |
||
+ | |||
+ | this.infoSystemChanged = function(to, from) |
||
+ | { |
||
+ | log(this.name, "infoSystemChanged(" + to + ", " + from + ")"); |
||
+ | } |
||
+ | |||
+ | this.infoSystemWillChange = function(to, from) |
||
+ | { |
||
+ | log(this.name, "infoSystemWillChange(" + to + ", " + from + ")"); |
||
+ | } |
||
+ | |||
+ | this.mfdKeyChanged = function(activeMFD, mfdKey) |
||
+ | { |
||
+ | log(this.name, "mfdKeyChanged(" + activeMFD + ", " + mfdKey + ")"); |
||
+ | } |
||
+ | |||
+ | this.missionChoiceWasReset= function() |
||
+ | { |
||
+ | log(this.name, "missionChoiceWasReset()"); |
||
+ | } |
||
+ | |||
+ | this.missionScreenEnded = function() |
||
+ | { |
||
+ | log(this.name, "missionScreenEnded()"); |
||
+ | } |
||
+ | |||
+ | this.missionScreenOpportunity= function() |
||
+ | { |
||
+ | log(this.name, "missionScreenOpportunity()"); |
||
+ | } |
||
+ | |||
+ | this.reportScreenEnded = function() |
||
+ | { |
||
+ | log(this.name, "reportScreenEnded()"); |
||
+ | } |
||
+ | |||
+ | this.selectedMFDChanged = function(activeMFD) |
||
+ | { |
||
+ | log(this.name, "selectedMFDChanged(" + activeMFD + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipCollided = function(otherShip) |
||
+ | { |
||
+ | log(this.name, "shipCollided(" + otherShip + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipSpawned = function(ship) |
||
+ | { |
||
+ | log(this.name, "shipSpawned(" + ship + ")"); |
||
+ | } |
||
+ | |||
+ | this.shipLaunchedEscapePod = function(escapepod) |
||
+ | { |
||
+ | log(this.name, "shipLaunchedEscapePod(" + escapepod + ")"); |
||
+ | } |
||
+ | |||
+ | |||
+ | this.systemInformationChanged = function(galaxy,system,key,newValue) |
||
+ | { |
||
+ | log(this.name, "systemInformationChanged(" + galaxy + ", " + system + ", " + key + ", " + newValue + ")"); |
||
+ | } |
||
+ | |||
+ | this.viewDirectionChanged = function(viewString) |
||
+ | { |
||
+ | log(this.name, "viewDirectionChanged(" + viewString + ")"); |
||
+ | } |
||
− | this.STATUS_LAUNCHING = function () { |
||
+ | </pre> |
||
− | } |
||
+ | === AppleMacs === |
||
+ | ... Strict semicolon parsing is a Mac thing. Windows and Linux use GNUstep to parse plists and that apparently is more relaxed syntax-wise than whatever it is that the Mac uses. [[User:Another_commander|Another_commander]] from [http://www.aegidian.org/bb/viewtopic.php?p=262814#p262814 A possible solution?] (2018). |
||
− | ==== STATUS_EXITING_WITCHSPACE ==== |
||
+ | == See Also == |
||
+ | * [http://www.aegidian.org/bb/viewtopic.php?f=4&t=8968 Understanding JavaScript, maybe] (Jens Ayton, 2011) |
||
+ | * [https://developer.mozilla.org/en/JavaScript/Reference Mozilla JavaScript reference pages] |
||
+ | * [[Oolite JavaScript event handler reference]] |
||
+ | * [[Oolite JavaScript object model]] |
||
+ | <!-- * [[JavaScript test OXP]] --> |
||
+ | * [[Variables in Oolite JavaScripts]] |
||
+ | * [[Javascript Operators]] |
||
+ | * [[Handling OXP Dependencies with JavaScript]] |
||
+ | * [[Optimization tips]] |
||
− | The <tt>STATUS_EXITING_WITCHSPACE</tt> event is called once when the player arrives in a new system. |
||
+ | === History === |
||
+ | * [http://aegidian.org/bb/viewtopic.php?f=6&t=2784 Anyone want to write scripts using JavaScript?] ([[David Taylor|Dajt]], 2007) |
||
− | this.STATUS_EXITING_WITCHSPACE = function () { |
||
+ | === Updating Javascript/Spidermonkey === |
||
− | } |
||
+ | * [http://www.aegidian.org/bb/viewtopic.php?p=275357#p275357 Issues with updating] (2021) |
||
− | [[Category:Oolite |
+ | [[Category:Oolite]] |
+ | [[Category:Oolite scripting]] |
Latest revision as of 21:09, 27 December 2021
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.
Contents
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.
Note: If you do not want to react to one of these events anyway you can fully remove that function. This list is complete now as it is easier for novice users to remove than to add.
this.name = "My OXP Script"; this.author = "Your Name Here"; this.copyright = "(C) 2021 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"; // Game State this.gamePaused = function() { log(this.name, "gamePaused()"); } this.gameResumed = function() { log(this.name, "gameResumed()"); } this.playerWillSaveGame = function() { log(this.name, "playerWillSaveGame()"); } this.startUp = function() { log(this.name, "startup()"); } this.startUpComplete = function() { log(this.name, "startupComplete()"); } // Docking this.shipWillDockWithStation = function(station) { log(this.name, "shipWillDockWithStation("+station+")"); } this.shipDockedWithStation = function(station) { log(this.name, "shipDockedWithStation("+station+")"); } this.shipWillLaunchFromStation = function(stationLaunchedFrom) { log(this.name, "shipWillLaunchFromStation("+stationLaunchedFrom+")"); } this.shipLaunchedFromStation = function(stationLaunchedFrom) { log(this.name, "shipLaunchedFromStation("+stationLaunchedFrom+")"); } this.playerStartedAutoPilot = function() { log(this.name, "playerStartedAutoPilot()"); } this.playerCancelledAutoPilot = function() { log(this.name, "playerCancelledAutoPilot()"); } this.playerDockingClearanceCancelled = function() { log(this.name, "playerDockingClearanceCancelled()"); } this.playerDockingClearanceExpired = function() { log(this.name, "playerDockingClearanceExpired()"); } this.playerDockingClearanceGranted = function() { log(this.name, "playerDockingClearanceGranted()"); } this.playerDockingRefused = function() { log(this.name, "playerDockingRefused()"); } this.playerRequestedDockingClearance = function(message) { log(this.name, "playerRequestedDockingClearance("+message+")"); } this.playerRescuedEscapePod = function(fee, reason, occupant) { log(this.name, "playerRescuedEscapePod("+fee + ", " + reason + ", " + occupant+")"); } this.playerCompletedContract = function(type, result, fee, contract) { log(this.name, "playerCompletedContract("+fee + ", " + reason + ", " + occupant+")"); } this.playerEnteredContract = function(type, contract) { log(this.name, "playerEnteredContract("+type + ", " + contract + ")"); } // Witchspace jumps this.playerStartedJumpCountdown = function(type, seconds) { log(this.name, "playerStartedJumpCountdown("+type + ", " + seconds + ")"); } this.playerCancelledJumpCountdown = function() { log(this.name, "playerCancelledJumpCountdown()"); } this.playerJumpFailed = function(reason) { log(this.name, "playerJumpFailed(" + reason + ")" ); } this.shipWillEnterWitchspace = function(cause, destination) { log(this.name, "shipWillEnterWitchspace(" + cause + ", " + destination + ")"); } this.shipWillExitWitchspace = function() { log(this.name, "shipWillEnterWitchspace()"); } this.shipExitedWitchspace = function() { log(this.name, "shipExitedWitchspace()"); } this.playerEnteredNewGalaxy = function(galaxyNumber) { log(this.name, "playerEnteredNewGalaxy(" + galaxyNumber + ")"); } // Enter/Exit Aegis this.shipEnteredStationAegis = function(station) { log(this.name, "shipEnteredStationAegis(" + station + ")"); } this.shipExitedStationAegis = function(station) { log(this.name, "shipExitedStationAegis(" + station + ")"); } this.shipEnteredPlanetaryVicinity = function(planet) { log(this.name, "shipEnteredPlanetaryVicinity(" + planet + ")"); } this.shipExitedPlanetaryVicinity = function(planet) { log(this.name, "shipExitedPlanetaryVicinity(" + planet + ")"); } this.shipApproachingPlanetSurface = function(planet) { log(this.name, "shipApproachingPlanetSurface(" + planet + ")"); } this.shipLeavingPlanetSurface = function(planet) { log(this.name, "shipLeavingPlanetSurface(" + planet + ")"); } // Combat this.alertConditionChanged = function(newCondition, oldCondition) { log(this.name, "alertConditionChanged(" + newCondition + ", " + oldCondition + ")"); } this.playerTargetedMissile = function(missile) { log(this.name, "playerTargetedMissile(" + missile + ")"); } this.shipAttackedOther = function(other) { log(this.name, "shipAttackedOther(" + other + ")"); } this.shipAttackedWithMissile = function(missile, whom) { log(this.name, "shipAttackedWithMissile(" + missile + ", " + whom + ")"); } this.shipBeingAttacked = function(whom) { log(this.name, "shipBeingAttacked(" + whom + ")"); } this.shipBeingAttackedByCloaked = function() { log(this.name, "shipBeingAttackedByCloaked()"); } this.shipKilledOther = function(whom, damageType) { log(this.name, "shipKilledOther(" + whom + ", " + damageType + ")"); } this.shipReleasedEquipment = function(mine) { log(this.name, "shipReleasedEquipment(" + mine + ")"); } this.shipTargetDestroyed = function(target) { log(this.name, "shipTargetDestroyed(" + target + ")"); } this.shipDied = function(whom, why) { log(this.name, "shipDied(" + whom + ", " + why + ")"); } this.shipFiredMissile = function(missile, target) { log(this.name, "shipFiredMissile(" + missile + ", " + target + ")"); } this.shipTargetLost = function(target) { log(this.name, "shipTargetLost(" + target + ")"); } this.shipTargetCloaked = function() { log(this.name, "shipTargetCloaked()"); } this.weaponsSystemsToggled = function(state) { log(this.name, "weaponsSystemsToggled(" + state + ")"); } // Equipment and Cargo this.equipmentAdded = function(equipmentKey) { log(this.name, "equipmentAdded(" + equipmentKey + ")"); } this.equipmentDamaged = function(equipment) { log(this.name, "equipmentDamaged(" + equipment + ")"); } this.equipmentRemoved = function(equipmentKey) { log(this.name, "equipmentRemoved(" + equipmentKey + ")"); } this.equipmentRepaired = function(equipment) { log(this.name, "equipmentRepaired(" + equipment + ")"); } this.playerBoughtCargo = function(commodity, units, price) { log(this.name, "playerBoughtCargo(" + commodity + ", " + units + ", " + price + ")"); } this.playerBoughtEquipment = function(equipment, paid) { log(this.name, "playerBoughtEquipment(" + equipment + ", " + paid + ")"); } this.playerBoughtNewShip = function(ship, price) { log(this.name, "playerBoughtNewShip(" + ship + ", " + price + ")"); } this.playerChangedPrimedEquipment = function(equipmentKey) { log(this.name, "playerChangedPrimedEquipment(" + equipmentKey + ")"); } this.playerReplacedShip = function(ship) { log(this.name, "playerReplacedShip(" + ship + ")"); } this.playerSoldCargo = function(commodity, units, price) { log(this.name, "playerSoldCargo(" + commodity + ", " + units + ", " + price + ")"); } this.shipScoopedFuel = function() { log(this.name, "shipScoopedFuel()"); } this.shipScoopedOther = function(whom) { log(this.name, "shipScoopedOther(" + whom + ")"); } this.playerWillBuyNewShip = function(dataKey, shipyard, price, tradeIn) { log(this.name, "playerWillBuyNewShip(" + dataKey + ", " + shipyard + ", " + price + ", " + tradeIn + ")"); } this.playerWillReplaceShip = function(dataKey) { log(this.name, "playerWillReplaceShip(" + dataKey + ")"); } // Other this.chartHightlightModeChanged = function(newMode) { log(this.name, "chartHightlightModeChanged(" + newMode + ")"); } this.compassTargetChanged = function(whom, mode) { log(this.name, "compassTargetChanged(" + whom + ", " + mode + ")"); } this.dayChanged = function(newday) { log(this.name, "dayChanged(" + newday + ")"); } this.escapePodSequenceOver = function() { log(this.name, "escapePodSequenceOver()"); } this.guiScreenChanged = function(to, from) { log(this.name, "guiScreenChanged(" + to + ", " + from + ")"); } this.guiScreenWillChange = function(to, from) { log(this.name, "guiScreenWillChange(" + to + ", " + from + ")"); } this.infoSystemChanged = function(to, from) { log(this.name, "infoSystemChanged(" + to + ", " + from + ")"); } this.infoSystemWillChange = function(to, from) { log(this.name, "infoSystemWillChange(" + to + ", " + from + ")"); } this.mfdKeyChanged = function(activeMFD, mfdKey) { log(this.name, "mfdKeyChanged(" + activeMFD + ", " + mfdKey + ")"); } this.missionChoiceWasReset= function() { log(this.name, "missionChoiceWasReset()"); } this.missionScreenEnded = function() { log(this.name, "missionScreenEnded()"); } this.missionScreenOpportunity= function() { log(this.name, "missionScreenOpportunity()"); } this.reportScreenEnded = function() { log(this.name, "reportScreenEnded()"); } this.selectedMFDChanged = function(activeMFD) { log(this.name, "selectedMFDChanged(" + activeMFD + ")"); } this.shipCollided = function(otherShip) { log(this.name, "shipCollided(" + otherShip + ")"); } this.shipSpawned = function(ship) { log(this.name, "shipSpawned(" + ship + ")"); } this.shipLaunchedEscapePod = function(escapepod) { log(this.name, "shipLaunchedEscapePod(" + escapepod + ")"); } this.systemInformationChanged = function(galaxy,system,key,newValue) { log(this.name, "systemInformationChanged(" + galaxy + ", " + system + ", " + key + ", " + newValue + ")"); } this.viewDirectionChanged = function(viewString) { log(this.name, "viewDirectionChanged(" + viewString + ")"); }
AppleMacs
... Strict semicolon parsing is a Mac thing. Windows and Linux use GNUstep to parse plists and that apparently is more relaxed syntax-wise than whatever it is that the Mac uses. Another_commander from A possible solution? (2018).
See Also
- Understanding JavaScript, maybe (Jens Ayton, 2011)
- Mozilla JavaScript reference pages
- Oolite JavaScript event handler reference
- Oolite JavaScript object model
- Variables in Oolite JavaScripts
- Javascript Operators
- Handling OXP Dependencies with JavaScript
- Optimization tips
History
Updating Javascript/Spidermonkey
- Issues with updating (2021)