Difference between revisions of "Oolite JavaScript Reference: World script event handlers"
(→equipmentAdded) |
Wildeblood (talk | contribs) m (→chartHightlightModeChanged) |
||
(55 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
− | This page provides a list of event handlers which can be implemented inside world scripts [[Scripting Oolite with JavaScript|JavaScript scripts for Oolite]]. Additionally, ship script handlers called on the player's ship will cause an equivalent world script event. | + | This page provides a list of event handlers which can be implemented inside world scripts [[Scripting Oolite with JavaScript|JavaScript scripts for Oolite]]. Additionally, [[Oolite_JavaScript_Reference:_Ship_script_event_handlers|ship script handlers]] called on the player's ship will cause an equivalent world script event. |
Most event handlers can be used both in world scripts and in ship scripts. Generally speaking, handlers starting with "ship" can be used for both scripts and those starting with "player" are meant only for the player (= can only be used in a world script). Exceptions on this rule are mentioned with the individual handlers below. | Most event handlers can be used both in world scripts and in ship scripts. Generally speaking, handlers starting with "ship" can be used for both scripts and those starting with "player" are meant only for the player (= can only be used in a world script). Exceptions on this rule are mentioned with the individual handlers below. | ||
− | World scripts can be either found inside Config\script.js, or be distinctly named .js files inside the Scripts directory - in the latter case, the | + | World scripts can be either found inside Config\script.js, or be distinctly named .js files inside the Scripts directory - in the latter case, the [[world-scripts.plist]] will list the active world scripts. |
World scripts are always active. | World scripts are always active. | ||
Line 33: | Line 33: | ||
==== <code>playerWillSaveGame</code> ==== | ==== <code>playerWillSaveGame</code> ==== | ||
− | The <code>playerWillSaveGame</code> handler is called whenever the player saves a game. The transferred message is one of the following strings: | + | The <code>playerWillSaveGame</code> handler is called whenever the player saves a game. The transferred message is one of the following strings: <code>"STANDARD_SAVE"</code>, <code>"AUTO_SAVE"</code> or <code>"QUICK_SAVE"</code>. |
− | this.playerWillSaveGame = function(message | + | this.playerWillSaveGame = function(message) |
{ | { | ||
// Your code here | // Your code here | ||
} | } | ||
− | Using this event is useful for storing temporary variables in missionVariables, just before the game gets saved. missionVariables are slow in use compared to normal JS variables, | + | Using this event is useful for storing temporary variables in <code>[[Oolite_JavaScript_Reference:_Global#missionVariables|missionVariables]]</code>, just before the game gets saved. <code>missionVariables</code> are slow in use compared to normal JS variables, therefore their use should be minimised for efficient code. The main benefit of using <code>missionVariables</code> is that they are saved in a saved game. See also [[Mission variable]]. |
==== <code>startUp</code> ==== | ==== <code>startUp</code> ==== | ||
Line 51: | Line 51: | ||
} | } | ||
− | Note that from Oolite 1.79 onwards the player | + | Note that from Oolite 1.79 onwards the player is in the main station while this handler runs but may be moved to another station soon after. |
+ | |||
+ | See also startUpComplete(). | ||
==== <code>startUpComplete</code> ==== | ==== <code>startUpComplete</code> ==== | ||
{{oolite-method-added|1.79}} | {{oolite-method-added|1.79}} | ||
+ | |||
+ | this.startUpComplete = function() | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
The <code>startUpComplete</code> handler is run at game startup after the initial population of the system has been complete, and after the player has been moved to the station recorded in their save game. The order of world events at game start is therefore: | The <code>startUpComplete</code> handler is run at game startup after the initial population of the system has been complete, and after the player has been moved to the station recorded in their save game. The order of world events at game start is therefore: | ||
Line 71: | Line 79: | ||
// Your code here | // Your code here | ||
} | } | ||
− | At this moment | + | At this moment <code>ship.dockedStation == null</code>, <code>ship.docked == true</code>, <code>ship.status == "STATUS_DOCKING"</code> |
Line 82: | Line 90: | ||
// Your code here | // Your code here | ||
} | } | ||
− | At this moment | + | At this moment <code>ship.dockedStation == station</code>, <code>ship.docked == true</code>, <code>ship.status == "STATUS_DOCKED"</code> and <code>guiScreen</code> is either <code>"GUI_SCREEN_STATUS"</code> or <code>"GUI_SCREEN_REPORT"</code>. However, any identical handler from an other oxp could have changed those values. Never count on it but double check when important. |
==== <code>shipWillLaunchFromStation</code> ==== | ==== <code>shipWillLaunchFromStation</code> ==== | ||
Line 92: | Line 100: | ||
// Your code here | // Your code here | ||
} | } | ||
− | At this moment | + | At this moment <code>ship.dockedStation == station</code>, <code>ship.docked == false</code>, <code>ship.status == "STATUS_LAUNCHING"</code>. |
==== <code>shipLaunchedFromStation</code> ==== | ==== <code>shipLaunchedFromStation</code> ==== | ||
− | The <code>shipLaunchedFromStation</code> handler is called at the end of the launch tunnel effect. | + | The <code>shipLaunchedFromStation</code> handler is called at the end of the launch tunnel effect. The handler's argument is the station which launched the ship. |
− | this.shipLaunchedFromStation = function( | + | this.shipLaunchedFromStation = function(stationLaunchedFrom) |
{ | { | ||
// Your code here | // Your code here | ||
} | } | ||
− | At this moment | + | |
+ | At this moment <code>ship.dockedStation == null</code>, <code>ship.docked == false</code>, <code>ship.status == "STATUS_IN_FLIGHT"</code>. | ||
==== <code>playerStartedAutoPilot</code> ==== | ==== <code>playerStartedAutoPilot</code> ==== | ||
− | The <code>playerStartedAutoPilot</code> handler is called when the player starts autopilot docking. It is not called for the instantaneous dock command. | + | The <code>playerStartedAutoPilot</code> handler is called when the player starts autopilot docking. It is not called for the instantaneous dock command. The handler is called with an argument containing the station entity that is the docking target. |
− | this.playerStartedAutoPilot = function() | + | this.playerStartedAutoPilot = function(stationForDocking) |
{ | { | ||
// Your code here | // Your code here | ||
} | } | ||
− | |||
==== <code>playerCancelledAutoPilot</code> ==== | ==== <code>playerCancelledAutoPilot</code> ==== | ||
Line 257: | Line 265: | ||
==== <code>shipWillEnterWitchspace</code> ==== | ==== <code>shipWillEnterWitchspace</code> ==== | ||
− | The <code>shipWillEnterWitchspace</code> handler is called immediately before a witchspace jump, while the player is still in the starting system. The <code>cause</code> parameter is a string specifying what sort of jump is | + | The <code>shipWillEnterWitchspace</code> handler is called immediately before a witchspace jump, while the player is still in the starting system. The <code>cause</code> parameter is a string specifying what sort of jump is occurring; currently, the possible values are <code>"standard jump"</code>, <code>"galactic jump"</code>, <code>"wormhole"</code> and <code>"carried"</code>. Other values may be added in future. |
// 1.80 or earlier | // 1.80 or earlier | ||
Line 272: | Line 280: | ||
// Your code here | // Your code here | ||
} | } | ||
+ | |||
+ | '''Note:''' If the <code>cause</code> is <code>"carried"</code> the <code>destination</code> parameter is <code>undefined</code>. This behaviour is a bug which has been fixed in version 1.91. | ||
==== <code>shipWillExitWitchspace</code> ==== | ==== <code>shipWillExitWitchspace</code> ==== | ||
Line 278: | Line 288: | ||
this.shipWillExitWitchspace = function() | this.shipWillExitWitchspace = function() | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
+ | In 1.91 or later the handler gains a cause parameter, describing the reason for the jump. The <code>cause</code> parameter is a string specifying what sort of jump is occurring; currently, the possible values are <code>"standard jump"</code>, <code>"galactic jump"</code>, <code>"wormhole"</code> and <code>"carried"</code>. Other values may be added in future. | ||
+ | |||
+ | // 1.91 or later | ||
+ | this.shipWillExitWitchspace = function(cause) | ||
{ | { | ||
// Your code here | // Your code here | ||
Line 287: | Line 305: | ||
this.shipExitedWitchspace = function() | this.shipExitedWitchspace = function() | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
+ | In 1.91 or later the handler gains a cause parameter, describing the reason for the jump. The <code>cause</code> parameter is a string specifying what sort of jump is occurring; currently, the possible values are <code>"standard jump"</code>, <code>"galactic jump"</code>, <code>"wormhole"</code> and <code>"carried"</code>. Other values may be added in future. | ||
+ | |||
+ | // 1.91 or later | ||
+ | this.shipExitedWitchspace = function(cause) | ||
{ | { | ||
// Your code here | // Your code here | ||
Line 307: | Line 333: | ||
// Your code here | // Your code here | ||
} | } | ||
+ | |||
+ | *[https://bb.oolite.space/viewtopic.php?f=4&t=18392 Order of JS events when galactic jumping] (2016) - mistakes in older information | ||
+ | *[[Global-settings.plist]] for Galactic Hyperspace Behaviour & co-ordinates | ||
=== Enter/Exit Aegis === | === Enter/Exit Aegis === | ||
Line 377: | Line 406: | ||
==== <code>playerTargetedMissile</code> ==== | ==== <code>playerTargetedMissile</code> ==== | ||
− | The <code>playerTargetedMissile</code> handler is called when the player targets the nearest missile by pressing T (shift-t) | + | The <code>playerTargetedMissile</code> handler is called when the player targets the nearest missile by pressing "T" (shift-t). |
this.playerTargetedMissile = function(missile) | this.playerTargetedMissile = function(missile) | ||
Line 410: | Line 439: | ||
// Your code here | // Your code here | ||
} | } | ||
+ | |||
+ | ==== <code>shipTakingDamage</code> ==== | ||
+ | |||
+ | Not yet documented - seen in [[Auto eject OXP]]. | ||
==== <code>shipBeingAttackedByCloaked</code> ==== | ==== <code>shipBeingAttackedByCloaked</code> ==== | ||
Line 421: | Line 454: | ||
==== <code>shipKilledOther</code> ==== | ==== <code>shipKilledOther</code> ==== | ||
+ | {{oolite-method-added|1.75}} | ||
− | The <code>shipKilledOther</code> handler is called when a ship kills an other ship. <code>whom</code> the identity of the ship that was killed. <code>damageType</code> is the type of damage. | + | The <code>shipKilledOther</code> handler is called when a ship kills an other ship. <code>whom</code> the identity of the ship that was killed. <code>damageType</code> is the type of damage. |
− | this.shipKilledOther = function(whom,damageType) | + | this.shipKilledOther = function(whom : ship, damageType : string) |
{ | { | ||
// Your code here | // Your code here | ||
Line 482: | Line 516: | ||
this.shipTargetCloaked = function() | this.shipTargetCloaked = function() | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
+ | ==== <code>weaponsSystemsToggled</code> ==== | ||
+ | {{oolite-method-added|1.85}} | ||
+ | |||
+ | The <code>weaponsSystemsToggled</code> handler is called whenever the player toggles their weapons systems on or off. The <code>state</code> parameter contains the new state of the weapons systems | ||
+ | |||
+ | this.weaponsSystemsToggled = function(state : boolean) | ||
{ | { | ||
// Your code here | // Your code here | ||
Line 504: | Line 548: | ||
The <code>equipmentDamaged</code> handler is called when equipment gets damaged. (world script only) | The <code>equipmentDamaged</code> handler is called when equipment gets damaged. (world script only) | ||
− | this.equipmentDamaged = function( | + | this.equipmentDamaged = function(equipmentKey) |
{ | { | ||
// Your code here | // Your code here | ||
Line 524: | Line 568: | ||
The <code>equipmentRepaired</code> handler is called when equipment gets repaired. (world script only) | The <code>equipmentRepaired</code> handler is called when equipment gets repaired. (world script only) | ||
− | this.equipmentRepaired = function( | + | this.equipmentRepaired = function(equipmentKey) |
{ | { | ||
// Your code here | // Your code here | ||
Line 543: | Line 587: | ||
The <code>playerBoughtEquipment</code> handler is called when equipment is bought at the outfit screen. | The <code>playerBoughtEquipment</code> handler is called when equipment is bought at the outfit screen. | ||
− | this.playerBoughtEquipment = function(equipment) | + | this.playerBoughtEquipment = function(equipment, paid) |
{ | { | ||
// Your code here | // Your code here | ||
} | } | ||
+ | |||
+ | The 'paid' parameter is added from 1.89 onwards. This is the amount of credits the player paid for the equipment, including any refunds that might have been applied during the purchase (eg when purchasing a laser and installing it in a position where a laser is already fitted, the cost of the current laser will be refunded to the player during the purchase). | ||
==== <code>playerBoughtNewShip</code> ==== | ==== <code>playerBoughtNewShip</code> ==== | ||
Line 557: | Line 603: | ||
} | } | ||
− | The 'price' parameter is added from 1.81 onwards. It is the cost of the ship (not counting any trade-in value of the player's old ship) in credits, or zero for changes using [[Oolite JavaScript Reference: Player#replaceShip|player.replaceShip]] | + | The 'price' parameter is added from 1.81 onwards. It is the cost of the ship (not counting any trade-in value of the player's old ship) in credits, or zero for changes using [[Oolite JavaScript Reference: Player#replaceShip|<code>player.replaceShip()</code>]] |
+ | |||
+ | '''Note:''' In a future release of Oolite, <code>playerBoughtNewShip</code> will no longer be fired when the ship is replaced using [[Oolite JavaScript Reference: Player#replaceShip|<code>player.replaceShip()</code>]]. Instead, the <code>[[#playerReplacedShip|playerReplacedShip]]</code> event should be used. At the moment, both events (<code>playerBoughtNewShip</code> and <code>playerReplacedShip</code>) will fire. | ||
+ | |||
+ | '''See also:''' <code>[[#playerWillBuyNewShip|playerWillBuyNewShip]]</code> | ||
==== <code>playerChangedPrimedEquipment</code> ==== | ==== <code>playerChangedPrimedEquipment</code> ==== | ||
Line 568: | Line 618: | ||
// Your code here | // Your code here | ||
} | } | ||
+ | |||
+ | ==== <code>playerReplacedShip</code> ==== | ||
+ | {{Oolite-method-added|1.89}} | ||
+ | |||
+ | The <code>playerReplacedShip</code> handler is called when the player ship is replaced via the JS method [[Oolite JavaScript Reference: Player#replaceShip|<code>player.replaceShip</code>]]. May be needed to re-evaluate the old equipment as replacing a ship does not trigger equipment removal. | ||
+ | |||
+ | this.playerReplacedShip = function(ship) | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
+ | '''See also:''' <code>[[#playerWillReplaceShip|playerWillReplaceShip]]</code> | ||
==== <code>playerSoldCargo</code> ==== | ==== <code>playerSoldCargo</code> ==== | ||
Line 597: | Line 659: | ||
// Your code here | // Your code here | ||
} | } | ||
+ | |||
+ | ==== <code>playerWillBuyNewShip</code> ==== | ||
+ | {{Oolite-method-added|1.89}} | ||
+ | |||
+ | The <code>playerWillBuyNewShip</code> handler is called just before a new ship is bought. May be needed to re-evaluate the old equipment as buying a new ship does not trigger equipment removal. | ||
+ | |||
+ | this.playerWillBuyNewShip = function(dataKey, shipyard, price, tradeIn) | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
+ | <code>dataKey</code> is the shipdata.plist <code>dataKey</code> for the ship being purchased. | ||
+ | |||
+ | <code>shipyard</code> is a dictionary object containing details about the ship being purchased, with information similar to the following: | ||
+ | { | ||
+ | short_description: "Python: Plus Galactic Hyperdrive. Extra Energy Unit. Docking Computers. E.C.M. System. Forward weapon upgraded to military laser. Price 215 000 ₢.", | ||
+ | shipdata_key: "python-player", | ||
+ | id: "675b6b-c60a74", | ||
+ | price: 215000, | ||
+ | ship: { | ||
+ | ...''[[shipdata.plist]]'' details of ship... | ||
+ | }, | ||
+ | personality: 9807, | ||
+ | extras: ["EQ_GAL_DRIVE", "EQ_FUEL_SCOOPS", "EQ_ENERGY_UNIT", "EQ_FUEL_INJECTION", "EQ_DOCK_COMP", "EQ_ECM"] | ||
+ | } | ||
+ | |||
+ | <code>price</code> is the amount being paid the for ship. | ||
+ | |||
+ | <code>tradeIn</code> is the trade in value of the current ship. | ||
+ | |||
+ | '''See also:''' <code>[[#playerBoughtNewShip|playerBoughtNewShip]]</code> | ||
+ | |||
+ | ==== <code>playerWillReplaceShip</code> ==== | ||
+ | {{Oolite-method-added|1.89}} | ||
+ | |||
+ | The <code>playerWillReplaceShip</code> handler is called just before a ship is replaced using [[Oolite JavaScript Reference: Player#replaceShip|<code>player.replaceShip()</code>]]. May be needed to re-evaluate the old equipment as buying a new ship does not trigger equipment removal. | ||
+ | |||
+ | this.playerWillReplaceShip = function(dataKey) | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
+ | <code>dataKey</code> is the shipdata.plist <code>dataKey</code> for the ship being purchased. | ||
+ | |||
+ | '''See also:''' <code>[[#playerReplacedShip|playerReplacedShip]]</code> | ||
=== Other === | === Other === | ||
+ | |||
+ | ==== <code>chartHighlightModeChanged</code> ==== | ||
+ | {{oolite-method-added|1.87}} | ||
+ | |||
+ | The <code>chartHighlightModeChanged</code> handler is called whenever the highlight mode of the galactic chart is changed either manually (from player input) or programmatically (by setting the mode with <code>[[Oolite JavaScript Reference: PlayerShip#chartHighlightMode|player.ship.chartHightlightMode]]</code>). The "newMode" parameter will be one of the following: | ||
+ | * <code>"OOLRC_MODE_SUNCOLOR"</code> | ||
+ | * <code>"OOLRC_MODE_ECONOMY"</code> | ||
+ | * <code>"OOLRC_MODE_GOVERNMENT"</code> | ||
+ | * <code>"OOLRC_MODE_TECHLEVEL"</code> | ||
+ | |||
+ | this.chartHightlightModeChanged = function(newMode) | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
==== <code>compassTargetChanged</code> ==== | ==== <code>compassTargetChanged</code> ==== | ||
Line 616: | Line 737: | ||
{ | { | ||
log(' Now targeting ' + whom); | log(' Now targeting ' + whom); | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
+ | ==== <code>consoleMessageReceived</code> ==== | ||
+ | {{oolite-method-added|1.91}} | ||
+ | |||
+ | The <code>consoleMessageReceived</code> handler is called whenever a console message is received. The "text" parameter will contain the text of the message. | ||
+ | |||
+ | this.consoleMessageReceived = function(text) | ||
+ | { | ||
// Your code here | // Your code here | ||
} | } | ||
Line 622: | Line 753: | ||
{{oolite-method-added|1.77}} | {{oolite-method-added|1.77}} | ||
− | The <code>dayChanged</code> handler is called each time a new day starts. At very low frame rates while the clock is updating, it is possible for this handler to be called twice in the same frame. Therefore, clock.days will not be correct for one of the calls. Use the day number passed to the | + | The <code>dayChanged</code> handler is called each time a new day starts. At very low frame rates while the clock is updating, it is possible for this handler to be called twice in the same frame. Therefore, clock.days will not be correct for one of the calls. Use the day number (newday parameter) passed to the handler instead. |
this.dayChanged = function(newday) | this.dayChanged = function(newday) | ||
Line 633: | Line 764: | ||
The <code>escapePodSequenceOver</code> handler is called at the end of the escape pod sequence, after the <code>shipLaunchedEscapePod</code> event and just prior to the <code>shipWillDockWithStation</code> and <code>shipDockedWithStation</code> events. | The <code>escapePodSequenceOver</code> handler is called at the end of the escape pod sequence, after the <code>shipLaunchedEscapePod</code> event and just prior to the <code>shipWillDockWithStation</code> and <code>shipDockedWithStation</code> events. | ||
− | Use this | + | Use this handler if you need to override the destination for the escape pod with [[Oolite_JavaScript_Reference:_Player#setEscapePodDestination|<code>player.setEscapePodDestination()</code>]]. |
this.escapePodSequenceOver = function() | this.escapePodSequenceOver = function() | ||
Line 642: | Line 773: | ||
==== <code>guiScreenChanged</code> ==== | ==== <code>guiScreenChanged</code> ==== | ||
− | The <code>guiScreenChanged</code> handler is called when the guiScreen changes. | + | The <code>guiScreenChanged</code> handler is called when the <code>[[Oolite JavaScript Reference: Global#guiScreen|guiScreen]]</code> changes. The ''from'' parameter is the screen it is leaving and ''to'' is the screen initially being switched to. Note that the screen can have changed again in the meantime by the action of other oxps. Therefore, it is generally better to test the global <code>[[Oolite JavaScript Reference: Global#guiScreen|guiScreen]]</code> to see which page is really on display instead of using the ''to'' parameter. (world script only) |
− | This handler will | + | |
+ | This handler will only fire when switched to/from any of the following: | ||
+ | |||
+ | 1.76: <code>"GUI_SCREEN_MAIN"</code>, <code>"GUI_SCREEN_STATUS"</code>(F5), <code>"GUI_SCREEN_MANIFEST"</code>(F5F5), <code>"GUI_SCREEN_SYSTEM_DATA"</code>(F7), <code>"GUI_SCREEN_OPTIONS"</code>, <code>"GUI_SCREEN_EQUIP_SHIP"</code>(F3), <code>"GUI_SCREEN_SHIPYARD"</code>(F3F3), <code>"GUI_SCREEN_SHORT_RANGE_CHART"</code>(F6), <code>"GUI_SCREEN_LONG_RANGE_CHART"</code>(F6F6), <code>"GUI_SCREEN_MARKET"</code>(F8), <code>"GUI_SCREEN_CONTRACTS"</code>, <code>"GUI_SCREEN_REPORT"</code> | ||
− | 1. | + | 1.77: adds <code>"GUI_SCREEN_INTERFACES"</code>(F4) and removes <code>"GUI_SCREEN_CONTRACTS"</code> |
− | 1. | + | 1.81: adds <code>"GUI_SCREEN_MARKETINFO"</code>(F8F8) |
this.guiScreenChanged = function(to, from) | this.guiScreenChanged = function(to, from) | ||
Line 656: | Line 790: | ||
==== <code>guiScreenWillChange</code> ==== | ==== <code>guiScreenWillChange</code> ==== | ||
− | The <code>guiScreenWillChange</code> handler is called when the guiScreen is about to change. | + | The <code>guiScreenWillChange</code> handler is called when the <code>[[Oolite JavaScript Reference: Global#guiScreen|guiScreen]]</code> is about to change. On these screens, a script could change the content of the page to be displayed. (world script only) |
+ | |||
+ | This handler will only fire when switching to/from any of the following: | ||
+ | |||
+ | <code>"GUI_SCREEN_EQUIP_SHIP"</code>(F3), <code>"GUI_SCREEN_MANIFEST"</code>(F5F5), <code>"GUI_SCREEN_MARKET"</code>(F8), <code>"GUI_SCREEN_SHIPYARD"</code>(F3F3), <code>"GUI_SCREEN_SYSTEM_DATA"</code>(F7) | ||
+ | |||
+ | 1.77: adds <code>"GUI_SCREEN_INTERFACES"</code>(F4), <code>"GUI_SCREEN_STATUS"</code>(F5) | ||
+ | |||
+ | 1.81: adds <code>"GUI_SCREEN_MARKETINFO"</code>(F8F8) | ||
this.guiScreenWillChange = function(to, from) | this.guiScreenWillChange = function(to, from) | ||
Line 676: | Line 818: | ||
{{oolite-method-added|1.85}} | {{oolite-method-added|1.85}} | ||
− | The <code> | + | The <code>infoSystemWillChange</code> handler is called just before the system displayed in F7 is updated, e.g. by moving the small blue cursor along the planned route in the chart. |
this.infoSystemWillChange = function(to, from) | this.infoSystemWillChange = function(to, from) | ||
Line 682: | Line 824: | ||
// Your code here | // Your code here | ||
} | } | ||
+ | |||
+ | ==== <code>mfdKeyChanged</code> ==== | ||
+ | {{oolite-method-added|1.85}} | ||
+ | |||
+ | The <code>mfdKeyChanged</code> handler is called whenever the player changes the content of an MFD slot. The <code>activeMFD</code> parameter identifies which MFD slot is being changed, and the <code>mfdKey</code> specifies the key code now in use in this slot. | ||
+ | |||
+ | this.mfdKeyChanged = function(activeMFD : integer, mfdKey : string) | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
+ | '''See also:''' <code>[[Oolite JavaScript Reference: PlayerShip#setMultiFunctionDisplay|PlayerShip.setMultiFunctionDisplay()]]</code> | ||
==== <code>missionChoiceWasReset</code> ==== | ==== <code>missionChoiceWasReset</code> ==== | ||
− | The <code>missionChoiceWasReset</code> handler is called when the mission choice is set to null via script (either using the legacy script method resetMissionChice | + | The <code>missionChoiceWasReset</code> handler is called when the mission choice is set to <code>null</code> via script (either using the legacy script method resetMissionChice or using <code>mission.runScreen ({choices: null})</code> in javascript) |
this.missionChoiceWasReset= function() | this.missionChoiceWasReset= function() | ||
Line 694: | Line 848: | ||
==== <code>missionScreenEnded</code> ==== | ==== <code>missionScreenEnded</code> ==== | ||
− | The <code>missionScreenEnded</code> handler is called when | + | The <code>missionScreenEnded</code> handler is called when a mission screen ends. Note that another script may have put up a new mission screen in the meantime. (world script only) |
this.missionScreenEnded = function() | this.missionScreenEnded = function() | ||
Line 703: | Line 857: | ||
==== <code>missionScreenOpportunity</code> ==== | ==== <code>missionScreenOpportunity</code> ==== | ||
− | The <code>missionScreenOpportunity</code> handler is called if there are no mission / report screens active | + | The <code>missionScreenOpportunity</code> handler is called if there are no mission/report screens active and the player's ship is docked. It gets fired at game startup, upon docking and after an arrival report or previous mission screen has ended. (world script only) |
this.missionScreenOpportunity= function() | this.missionScreenOpportunity= function() | ||
Line 710: | Line 864: | ||
} | } | ||
− | This handler works a bit different | + | This handler works a bit different from other handlers. It fires for every installed OXP/OXZ until one creates a mission screen during this handler, and then it stops. When the mission screen ends and there is no callback function that creates a new mission screen, the <code>missionScreenOpportunity</code> is sent again to all installed OXPs/OXZs starting with the one that created the last mission screen. All this means is that when this handler fires, it is safe to show a mission screen and no further tests are needed. |
==== <code>reportScreenEnded</code> ==== | ==== <code>reportScreenEnded</code> ==== | ||
Line 717: | Line 871: | ||
this.reportScreenEnded = function() | this.reportScreenEnded = function() | ||
+ | { | ||
+ | // Your code here | ||
+ | } | ||
+ | |||
+ | ==== <code>selectedMFDChanged</code> ==== | ||
+ | {{oolite-method-added|1.85}} | ||
+ | |||
+ | The <code>selectedMFDChanged</code> handler is send whenever the player selects a new MFD slot (normally using the default keypress ";"). The <code>activeMFD</code> paramater is the index of the MFD which is now active. | ||
+ | |||
+ | this.selectedMFDChanged = function(activeMFD : integer) | ||
{ | { | ||
// Your code here | // Your code here | ||
Line 733: | Line 897: | ||
The <code>shipSpawned</code> handler is called after an NPC ship has been added to the system. After a witchspace jump it means that first all ships are added to the system, then all the relevant shipSpawned events are triggered.<br> | The <code>shipSpawned</code> handler is called after an NPC ship has been added to the system. After a witchspace jump it means that first all ships are added to the system, then all the relevant shipSpawned events are triggered.<br> | ||
− | This handler for the | + | This handler for the worldScript is new since Oolite 1.74. After the event is sent to the shipScript, it is now also send to the worldScript with the added entity as argument. |
this.shipSpawned = function(ship) | this.shipSpawned = function(ship) | ||
Line 754: | Line 918: | ||
The <code>systemInformationChanged</code> handler is called when system information is modified. It is passed the galaxy and system ID which were changed, and the key and new value in the SystemInfo object. | The <code>systemInformationChanged</code> handler is called when system information is modified. It is passed the galaxy and system ID which were changed, and the key and new value in the SystemInfo object. | ||
− | To avoid problems with recursion, attempting to change the value of any system information property from within this | + | To avoid problems with recursion, attempting to change the value of any system information property from within this handler will fail and log an error. Also note that changes which take place while Oolite is not running (from OXP planetinfo.plist files) will not cause this handler to be called when the game is reloaded. |
this.systemInformationChanged = function(galaxy,system,key,newValue) | this.systemInformationChanged = function(galaxy,system,key,newValue) | ||
Line 767: | Line 931: | ||
"VIEW_GUI_DISPLAY". | "VIEW_GUI_DISPLAY". | ||
+ | '''Note:''' [https://bb.oolite.space/viewtopic.php?f=4&t=10602 viewDirectionChanged woes] (2011) | ||
this.viewDirectionChanged = function(viewString) | this.viewDirectionChanged = function(viewString) | ||
{ | { | ||
Line 799: | Line 964: | ||
All the initially planned events have been implemented in 1.74. | All the initially planned events have been implemented in 1.74. | ||
− | If there are other events you would like to be able to respond to, please write a request [ | + | If there are other events you would like to be able to respond to, please write a request [https://bb.oolite.space/viewtopic.php?t=3296 on the forum]. |
'''See also:''' [[Oolite JavaScript Reference: Ship script event handlers|ship script event handlers]] | '''See also:''' [[Oolite JavaScript Reference: Ship script event handlers|ship script event handlers]] | ||
− | [[Category:Oolite JavaScript Reference]] | + | [[Category:Oolite JavaScript Reference]][[Category:Updated JavaScript features in Oolite 1.91]] |
Latest revision as of 03:08, 27 August 2024
This page provides a list of event handlers which can be implemented inside world scripts JavaScript scripts for Oolite. Additionally, ship script handlers called on the player's ship will cause an equivalent world script event.
Most event handlers can be used both in world scripts and in ship scripts. Generally speaking, handlers starting with "ship" can be used for both scripts and those starting with "player" are meant only for the player (= can only be used in a world script). Exceptions on this rule are mentioned with the individual handlers below.
World scripts can be either found inside Config\script.js, or be distinctly named .js files inside the Scripts directory - in the latter case, the world-scripts.plist will list the active world scripts.
World scripts are always active.
The list of event handlers will change from version to version (usually additions of extra handlers). For this reason, any variables or functions you create as this.variable
should have a name beginning with '_' or '$' - e.g. this._variable
or this.$variable
- to avoid potential conflicts with future event handlers (which will never start with '_' or '$').
Contents
- 1 Game State
- 2 Docking
- 2.1 shipWillDockWithStation
- 2.2 shipDockedWithStation
- 2.3 shipWillLaunchFromStation
- 2.4 shipLaunchedFromStation
- 2.5 playerStartedAutoPilot
- 2.6 playerCancelledAutoPilot
- 2.7 playerDockingClearanceCancelled
- 2.8 playerDockingClearanceExpired
- 2.9 playerDockingClearanceGranted
- 2.10 playerDockingRefused
- 2.11 playerRequestedDockingClearance
- 2.12 playerRescuedEscapePod
- 2.13 playerCompletedContract
- 2.14 playerEnteredContract
- 3 Witchspace Jumps
- 4 Enter/Exit Aegis
- 5 Combat
- 5.1 alertConditionChanged
- 5.2 playerTargetedMissile
- 5.3 shipAttackedOther
- 5.4 shipAttackedWithMissile
- 5.5 shipBeingAttacked
- 5.6 shipTakingDamage
- 5.7 shipBeingAttackedByCloaked
- 5.8 shipKilledOther
- 5.9 shipReleasedEquipment
- 5.10 shipTargetDestroyed
- 5.11 shipDied
- 5.12 shipFiredMissile
- 5.13 shipTargetLost
- 5.14 shipTargetCloaked
- 5.15 weaponsSystemsToggled
- 6 Equipment and Cargo
- 6.1 equipmentAdded
- 6.2 equipmentDamaged
- 6.3 equipmentRemoved
- 6.4 equipmentRepaired
- 6.5 playerBoughtCargo
- 6.6 playerBoughtEquipment
- 6.7 playerBoughtNewShip
- 6.8 playerChangedPrimedEquipment
- 6.9 playerReplacedShip
- 6.10 playerSoldCargo
- 6.11 shipScoopedFuel
- 6.12 shipScoopedOther
- 6.13 playerWillBuyNewShip
- 6.14 playerWillReplaceShip
- 7 Other
- 7.1 chartHighlightModeChanged
- 7.2 compassTargetChanged
- 7.3 consoleMessageReceived
- 7.4 dayChanged
- 7.5 escapePodSequenceOver
- 7.6 guiScreenChanged
- 7.7 guiScreenWillChange
- 7.8 infoSystemChanged
- 7.9 infoSystemWillChange
- 7.10 mfdKeyChanged
- 7.11 missionChoiceWasReset
- 7.12 missionScreenEnded
- 7.13 missionScreenOpportunity
- 7.14 reportScreenEnded
- 7.15 selectedMFDChanged
- 7.16 shipCollided
- 7.17 shipSpawned
- 7.18 shipLaunchedEscapePod
- 7.19 systemInformationChanged
- 7.20 viewDirectionChanged
- 8 System population
- 9 Defunct
- 10 Missing Events
Game State
gamePaused
This method was added in Oolite test release 1.81.
This event is called when the game is paused. This is mainly useful for pausing sound playback which should not continue while the game is paused.
this.gamePaused = function() { // Your code here }
gameResumed
This method was added in Oolite test release 1.81.
This event is called when the game is resumed from pause. This is mainly useful for resuming sound playback paused in a gamePaused
handler.
this.gameResumed = function() { // Your code here }
playerWillSaveGame
The playerWillSaveGame
handler is called whenever the player saves a game. The transferred message is one of the following strings: "STANDARD_SAVE"
, "AUTO_SAVE"
or "QUICK_SAVE"
.
this.playerWillSaveGame = function(message) { // Your code here }
Using this event is useful for storing temporary variables in missionVariables
, just before the game gets saved. missionVariables
are slow in use compared to normal JS variables, therefore their use should be minimised for efficient code. The main benefit of using missionVariables
is that they are saved in a saved game. See also Mission variable.
startUp
The startUp
handler is called after all OXPs have been loaded. This also means that it is called every time the player loads a game, begins a new game or presses space after dying. It can be used to do one-off initialisation such as copying mission variables into this.*
properties for efficiency or setting up data arrays to be used by the script in other handlers. (world script only)
this.startUp = function() { // Your code here }
Note that from Oolite 1.79 onwards the player is in the main station while this handler runs but may be moved to another station soon after.
See also startUpComplete().
startUpComplete
This method was added in Oolite test release 1.79.
this.startUpComplete = function() { // Your code here }
The startUpComplete
handler is run at game startup after the initial population of the system has been complete, and after the player has been moved to the station recorded in their save game. The order of world events at game start is therefore:
startUp
systemWillPopulate
(or an alternative handler specified by the system info)startUpComplete
Docking
shipWillDockWithStation
The shipWillDockWithStation
handler is called at the beginning of the docking tunnel effect.
this.shipWillDockWithStation = function(station) { // Your code here }
At this moment ship.dockedStation == null
, ship.docked == true
, ship.status == "STATUS_DOCKING"
shipDockedWithStation
The shipDockedWithStation
handler is called at the end of the docking tunnel effect.
this.shipDockedWithStation = function(station) { // Your code here }
At this moment ship.dockedStation == station
, ship.docked == true
, ship.status == "STATUS_DOCKED"
and guiScreen
is either "GUI_SCREEN_STATUS"
or "GUI_SCREEN_REPORT"
. However, any identical handler from an other oxp could have changed those values. Never count on it but double check when important.
shipWillLaunchFromStation
The shipWillLaunchFromStation
handler is called at the beginning of the launch tunnel effect.
this.shipWillLaunchFromStation = function(station) { // Your code here }
At this moment ship.dockedStation == station
, ship.docked == false
, ship.status == "STATUS_LAUNCHING"
.
shipLaunchedFromStation
The shipLaunchedFromStation
handler is called at the end of the launch tunnel effect. The handler's argument is the station which launched the ship.
this.shipLaunchedFromStation = function(stationLaunchedFrom) { // Your code here }
At this moment ship.dockedStation == null
, ship.docked == false
, ship.status == "STATUS_IN_FLIGHT"
.
playerStartedAutoPilot
The playerStartedAutoPilot
handler is called when the player starts autopilot docking. It is not called for the instantaneous dock command. The handler is called with an argument containing the station entity that is the docking target.
this.playerStartedAutoPilot = function(stationForDocking) { // Your code here }
playerCancelledAutoPilot
The playerCancelledAutoPilot
handler is called when the player cancels autopilot docking.
this.playerCancelledAutoPilot = function() { // Your code here }
playerDockingClearanceCancelled
This method was added in Oolite test release 1.85.
The playerDockingClearanceCancelled
handler is called when the player withdraws their request to dock at a station.
this.playerDockingClearanceCancelled = function() { // Your code here }
playerDockingClearanceExpired
This method was added in Oolite test release 1.83.
The playerDockingClearanceExpired
handler is called when the player exceeds the two minute window without requesting an extension
this.playerDockingClearanceExpired = function() { // Your code here }
playerDockingClearanceGranted
This method was added in Oolite test release 1.85.
The playerDockingClearanceGranted
handler is called when the player is given permission to dock at a station. If the player is given immediate clearance to dock, this event will be called immediately after the playerRequestedDockingClearance
event. If, however, the station is unable to give immediate clearance, then there will be some delay between the request to dock and permission being granted. This event will be called when the station actually gives docking clearance.
this.playerDockingClearanceGranted = function() { // Your code here }
playerDockingRefused
The playerDockingRefused
handler is called when a station refuses to provide autopilot docking instructions.
this.playerDockingRefused = function() { // Your code here }
playerRequestedDockingClearance
The playerRequestedDockingClearance
handler is called when a station answers on a docking request of the player by targeting the station and pressing L (shift-l).
this.playerRequestedDockingClearance = function(message) { // Your code here }
Message is a string and can take the values: "DOCKING_CLEARANCE_GRANTED", "DOCKING_CLEARANCE_DENIED_TRAFFIC_OUTBOUND", "DOCKING_CLEARANCE_DENIED_TRAFFIC_INBOUND", "DOCKING_CLEARANCE_DENIED_SHIP_FUGITIVE", "DOCKING_CLEARANCE_NOT_REQUIRED", "DOCKING_CLEARANCE_EXTENDED" or "DOCKING_CLEARANCE_CANCELLED".
playerRescuedEscapePod
This method was added in Oolite test release 1.81.
The playerRescuedEscapePod
handler is called when the player rescues an escape pod which does not have scripted content (all consequences of scripted content escape pods are assumed to be dealt with by the specified script instead). It has three parameters:
- the rescue fee, in decicredits
- the fee reason, which can be "insurance", "bounty" or "slave" (in the latter case, the fee will always be zero)
- a dictionary like those in the
ship.crew
property describing the pod occupant
this.playerRescuedEscapePod = function(fee, reason, occupant) { // Your code here }
This method is called after the escape pod has been processed, but slightly before the arrival report screen giving the results of the processing is displayed to the player.
playerCompletedContract
This method was added in Oolite test release 1.81.
The playerCompletedContract
handler is called when a passenger, parcel or cargo contract ends, either successfully or by defaulting on it. It is not called when all contracts are cancelled on a galactic jump. It has four parameters:
- The type of contract, which can be "cargo", "parcel" or "passenger"
- The result of the contract, which can be "success", "late", "failed", or for cargo contracts only "short"
- The fee paid for the contract in decicredits
- The contract information dictionary
Note that the fee actually paid for the contract will often not match the originally agreed fee in the contract information dictionary, as penalties for late delivery or bonuses for good service are included in the fee parameter.
this.playerCompletedContract = function(type, result, fee, contract) { // Your code here }
This method is called after the contract has been processed, but slightly before the arrival report screen giving the results of the processing is displayed to the player.
playerEnteredContract
This method was added in Oolite test release 1.81.
The playerEnteredContract
handler is called when a passenger, parcel or cargo contract starts, just after the items have been transferred to the player ship. It has two parameters:
- The type of contract, which can be "cargo", "parcel" or "passenger"
- The contract information dictionary
this.playerEnteredContract = function(type, contract) { // Your code here }
Witchspace Jumps
playerStartedJumpCountdown
The playerStartedJumpCountdown
handler is called when the user starts a witchspace or galactic witchspace jump countdown. The type
parameter is a string specifying which type of jump is occuring; currently, the possible values are “standard” and “galactic”. Other values may be added in future. The seconds
parameter is a number specifying the number of seconds the countdown is running for.
this.playerStartedJumpCountdown = function(type, seconds) { // Your code here }
playerCancelledJumpCountdown
The playerCancelledJumpCountdown
handler is called when the user cancels a witchspace or galactic witchspace jump countdown.
this.playerCancelledJumpCountdown = function() { // Your code here }
playerJumpFailed
The playerJumpFailed
handler is called at the end of a witchspace or galactic witchspace countdown, if the jump is not possible. The reason
parameter is a string specifying why the jump failed. The current values are:
- "insufficient fuel" - the ship no longer has enough fuel to make the jump (e.g. injector use or fuel leak).
- "blocked" - a heavy ship is near the player (specifically
object mass
/object distance
2 >= 10.0, andobject distance
<= scanner range). - "malfunction" - the jump malfunctioned in a way that leaves the player ship in the current system.
- "malfunction" - the Galactic Hyperdrive is damaged or removed during a galactic jump countdown.
Also theoretically possible but unlikely in current Oolite:
- "too far" - the jump destination is outside the 7LY range (but wasn't when the countdown started).
- "no target" - the jump destination is the current location (but wasn't when the countdown started).
this.playerJumpFailed = function(reason) { // Your code here }
shipWillEnterWitchspace
The shipWillEnterWitchspace
handler is called immediately before a witchspace jump, while the player is still in the starting system. The cause
parameter is a string specifying what sort of jump is occurring; currently, the possible values are "standard jump"
, "galactic jump"
, "wormhole"
and "carried"
. Other values may be added in future.
// 1.80 or earlier this.shipWillEnterWitchspace = function(cause) { // Your code here }
In 1.81 or later the handler gains a second parameter, describing the jump destination. For standard and wormhole jumps, this will be the system ID of the destination system. For galactic jumps, this will be the galaxy ID of the destination galaxy. (As player.ship.galacticHyperspaceBehaviour may be changed during shipWillEnterWitchspace
, it is not possible to predict in advance of a galactic jump being made what the resulting system ID in the destination galaxy will be)
// 1.81 or later this.shipWillEnterWitchspace = function(cause, destination) { // Your code here }
Note: If the cause
is "carried"
the destination
parameter is undefined
. This behaviour is a bug which has been fixed in version 1.91.
shipWillExitWitchspace
The shipWillExitWitchspace
handler is called as a witchspace jump concludes. When it is called, the player is (from a program perspective) in the destination system, but the tunnel effect has not yet been shown. Use this event to set up elements which need to be present in-system after the player exits witchspace.
this.shipWillExitWitchspace = function() { // Your code here }
In 1.91 or later the handler gains a cause parameter, describing the reason for the jump. The cause
parameter is a string specifying what sort of jump is occurring; currently, the possible values are "standard jump"
, "galactic jump"
, "wormhole"
and "carried"
. Other values may be added in future.
// 1.91 or later this.shipWillExitWitchspace = function(cause) { // Your code here }
shipExitedWitchspace
The shipExitedWitchspace
handler is called after a witchspace jump has concluded and the tunnel effect has been shown.
this.shipExitedWitchspace = function() { // Your code here }
In 1.91 or later the handler gains a cause parameter, describing the reason for the jump. The cause
parameter is a string specifying what sort of jump is occurring; currently, the possible values are "standard jump"
, "galactic jump"
, "wormhole"
and "carried"
. Other values may be added in future.
// 1.91 or later this.shipExitedWitchspace = function(cause) { // Your code here }
playerEnteredNewGalaxy
The playerEnteredNewGalaxy
handler is called just before shipWillExitWitchspace.
the sequence of events for a player jumping to a different galaxy is as follows:
shipWillEnterWitchspace (world event)
playerWillEnterWitchspace (NPC ship event)
playerEnteredNewGalaxy (world event)
shipWillExitWitchspace (world event)
shipExitedWitchspace (world event)
this.playerEnteredNewGalaxy = function(galaxyNumber) { // Your code here }
- Order of JS events when galactic jumping (2016) - mistakes in older information
- Global-settings.plist for Galactic Hyperspace Behaviour & co-ordinates
Enter/Exit Aegis
shipEnteredStationAegis
The shipEnteredStationAegis
handler is called when the player enters the aegis of the main-station (2x scanner range from main-station). Other stations than the main-station don't give aegis messages.
this.shipEnteredStationAegis = function(station) { // Your code here }
shipExitedStationAegis
The shipExitedStationAegis
handler is called when the player leaves the aegis of the main-station (2x scanner range from main-station).
this.shipExitedStationAegis = function(station) { // Your code here }
shipEnteredPlanetaryVicinity
The shipEnteredPlanetaryVicinity
handler is called when the player enters the planet aegis (3x planet radius).
this.shipEnteredPlanetaryVicinity = function(planet) { // Your code here }
shipExitedPlanetaryVicinity
The shipExitedPlanetaryVicinity
handler is called when the player leaves the planet aegis (3x planet radius).
this.shipExitedPlanetaryVicinity = function(planet) { // Your code here }
shipApproachingPlanetSurface
The shipApproachingPlanetSurface
handler is called when the player is very close to the planet (crosses a border ± 500 meter above the surface).
this.shipApproachingPlanetSurface = function(planet) { // Your code here }
shipLeavingPlanetSurface
The shipLeavingPlanetSurface
handler is called when the player leaves the planet (crosses a border ± 500 meter above the surface).
this.shipLeavingPlanetSurface = function(planet) { // Your code here }
Combat
alertConditionChanged
The alertConditionChanged
handler is called when the player’s alert status (player.alertCondition
) changes. Only the player and stations have an alert condition. (world script and station scripts)
this.alertConditionChanged = function(newCondition, oldCondition) { // Your code here }
playerTargetedMissile
The playerTargetedMissile
handler is called when the player targets the nearest missile by pressing "T" (shift-t).
this.playerTargetedMissile = function(missile) { // Your code here }
shipAttackedOther
The shipAttackedOther
handler is called when the player hits another with a laser shot. other
is the identity of the ship being hit.
this.shipAttackedOther = function(other) { // Your code here }
shipAttackedWithMissile
The shipAttackedWithMissile
handler is called when a missile is fired. missile
contains the missile entity and whom
the identity of the ship that launched the missile.
this.shipAttackedWithMissile = function(missile, whom) { // Your code here }
shipBeingAttacked
The shipBeingAttacked
handler is called when a laser shot hits. whom
the identity of the ship that attacked.
this.shipBeingAttacked = function(whom) { // Your code here }
shipTakingDamage
Not yet documented - seen in Auto eject OXP.
shipBeingAttackedByCloaked
The shipBeingAttackedByCloaked
handler is called when a laser shot from a cloaked ship hits. Guess what, there is no parameter because he is cloaked!
this.shipBeingAttackedByCloaked = function() { // Your code here }
shipKilledOther
This method was added in Oolite test release 1.75.
The shipKilledOther
handler is called when a ship kills an other ship. whom
the identity of the ship that was killed. damageType
is the type of damage.
this.shipKilledOther = function(whom : ship, damageType : string) { // Your code here }
shipReleasedEquipment
The shipReleasedEquipment
handler is called when a ship launches a mine (a pylon-mounted equipment item whose key ends with "_MINE"). mine
contains the launched mine entity.
this.shipReleasedEquipment = function(mine) { // Your code here }
shipTargetDestroyed
The shipTargetDestroyed
handler is called when the target gets destroyed by the player. target
contains the destroyed target entity. This command is always preceded by the shipTargetLost
handler.
this.shipTargetDestroyed = function(target) { // Your code here }
shipDied
The shipDied
handler is called when the ship or player dies. Expect a reset()
shortly when it is the player ship.
this.shipDied = function(whom, why) { // Your code here }
whom contains the entity that caused the kill. why is the cause written as string and is one of: "removed", "hit a planet", "energy damage", "scrape damage", "heat damage", "cascade weapon".
("cascade weapon" is new in 1.74 and "removed" / "energy damage" were accidentally switched in 1.73)
shipFiredMissile
The shipFiredMissile
handler is called when a missile is fired. missile
contains the missile entity and target
the identity of the target. The handler is send to the ship that launched the missile.
this.shipFiredMissile = function(missile, target) { // Your code here }
shipTargetLost
The shipTargetLost
handler is called when the target gets lost. target
contains the lost target entity.
this.shipTargetLost = function(target) { // Your code here }
shipTargetCloaked
The shipTargetCloaked
handler is called when the target cloakes.
this.shipTargetCloaked = function() { // Your code here }
weaponsSystemsToggled
This method was added in Oolite test release 1.85.
The weaponsSystemsToggled
handler is called whenever the player toggles their weapons systems on or off. The state
parameter contains the new state of the weapons systems
this.weaponsSystemsToggled = function(state : boolean) { // Your code here }
Equipment and Cargo
equipmentAdded
This method was added in Oolite test release 1.81.
The equipmentAdded
handler is called whenever the player ship gains an item of equipment. This includes "gaining" of EQ_SOMETHING_DAMAGED
when an EQ_SOMETHING
is damaged. This event will fire regardless of the reason for the equipment being added to the ship.
this.equipmentAdded = function(equipmentKey) { // Your code here }
This is often more convenient than monitoring both equipmentRepaired
and playerBoughtEquipment
, and will also detect equipment addition by script.
equipmentDamaged
The equipmentDamaged
handler is called when equipment gets damaged. (world script only)
this.equipmentDamaged = function(equipmentKey) { // Your code here }
equipmentRemoved
This method was added in Oolite test release 1.81.
The equipmentRemoved
handler is called whenever the player ship loses an item of equipment. This includes "losing" of EQ_SOMETHING_DAMAGED
when an EQ_SOMETHING
is repaired. This event will fire regardless of the reason for the equipment being removed from the ship.
this.equipmentRemoved = function(equipmentKey) { // Your code here }
equipmentRepaired
This method was added in Oolite test release 1.77.
The equipmentRepaired
handler is called when equipment gets repaired. (world script only)
this.equipmentRepaired = function(equipmentKey) { // Your code here }
playerBoughtCargo
This method was added in Oolite test release 1.77.
The playerBoughtCargo
handler is called when cargo is bought at the market screen. commodity
contains the non-localised name for the cargo. You can get the localised name using expandDescription("[commodity-name "+commodity+"]");
. price
is price per unit in tenths of a credit.
this.playerBoughtCargo = function(commodity, units, price) { // Your code here }
playerBoughtEquipment
The playerBoughtEquipment
handler is called when equipment is bought at the outfit screen.
this.playerBoughtEquipment = function(equipment, paid) { // Your code here }
The 'paid' parameter is added from 1.89 onwards. This is the amount of credits the player paid for the equipment, including any refunds that might have been applied during the purchase (eg when purchasing a laser and installing it in a position where a laser is already fitted, the cost of the current laser will be refunded to the player during the purchase).
playerBoughtNewShip
The playerBoughtNewShip
handler is called when a new ship is bought. May be needed to re-evaluate the old equipment as buying a new ship does not trigger equipment removal.
this.playerBoughtNewShip = function(ship, price) { // Your code here }
The 'price' parameter is added from 1.81 onwards. It is the cost of the ship (not counting any trade-in value of the player's old ship) in credits, or zero for changes using player.replaceShip()
Note: In a future release of Oolite, playerBoughtNewShip
will no longer be fired when the ship is replaced using player.replaceShip()
. Instead, the playerReplacedShip
event should be used. At the moment, both events (playerBoughtNewShip
and playerReplacedShip
) will fire.
See also: playerWillBuyNewShip
playerChangedPrimedEquipment
This method was added in Oolite test release 1.85.
The playerChangedPrimedEquipment
handler is called whenever the player changes the currently primed equipment (either with shift-n or ctrl-shift-n). The equipment key of the newly primed equipment will be passed as an argument.
this.playerChangedPrimedEquipment = function(equipmentKey) { // Your code here }
playerReplacedShip
This method was added in Oolite test release 1.89.
The playerReplacedShip
handler is called when the player ship is replaced via the JS method player.replaceShip
. May be needed to re-evaluate the old equipment as replacing a ship does not trigger equipment removal.
this.playerReplacedShip = function(ship) { // Your code here }
See also: playerWillReplaceShip
playerSoldCargo
This method was added in Oolite test release 1.77.
The playerSoldCargo
handler is called when cargo is sold at the market screen. commodity
contains the non-localised name for the cargo. You can get the localised name using expandDescription("[commodity-name "+commodity+"]");
. price
is price per unit in tenths of a credit.
this.playerSoldCargo = function(commodity, units, price) { // Your code here }
shipScoopedFuel
This method was added in Oolite test release 1.77.
The shipScoopedFuel
handler is called whenever the player's ship transfers 0.1LY of fuel from the scoops to the tank.
this.shipScoopedFuel = function() { // Your code here }
shipScoopedOther
The shipScoopedOther
handler is called when a ship scoops cargo. The scooped item is transferred as argument.
If the cargo is scripted cargo, but not otherwise, then the scooped cargo itself gets the handler shipWasScooped
with the scooper as argument.
this.shipScoopedOther = function(whom) { // Your code here }
playerWillBuyNewShip
This method was added in Oolite test release 1.89.
The playerWillBuyNewShip
handler is called just before a new ship is bought. May be needed to re-evaluate the old equipment as buying a new ship does not trigger equipment removal.
this.playerWillBuyNewShip = function(dataKey, shipyard, price, tradeIn) { // Your code here }
dataKey
is the shipdata.plist dataKey
for the ship being purchased.
shipyard
is a dictionary object containing details about the ship being purchased, with information similar to the following:
{ short_description: "Python: Plus Galactic Hyperdrive. Extra Energy Unit. Docking Computers. E.C.M. System. Forward weapon upgraded to military laser. Price 215 000 ₢.", shipdata_key: "python-player", id: "675b6b-c60a74", price: 215000, ship: { ...shipdata.plist details of ship... }, personality: 9807, extras: ["EQ_GAL_DRIVE", "EQ_FUEL_SCOOPS", "EQ_ENERGY_UNIT", "EQ_FUEL_INJECTION", "EQ_DOCK_COMP", "EQ_ECM"] }
price
is the amount being paid the for ship.
tradeIn
is the trade in value of the current ship.
See also: playerBoughtNewShip
playerWillReplaceShip
This method was added in Oolite test release 1.89.
The playerWillReplaceShip
handler is called just before a ship is replaced using player.replaceShip()
. May be needed to re-evaluate the old equipment as buying a new ship does not trigger equipment removal.
this.playerWillReplaceShip = function(dataKey) { // Your code here }
dataKey
is the shipdata.plist dataKey
for the ship being purchased.
See also: playerReplacedShip
Other
chartHighlightModeChanged
This method was added in Oolite test release 1.87.
The chartHighlightModeChanged
handler is called whenever the highlight mode of the galactic chart is changed either manually (from player input) or programmatically (by setting the mode with player.ship.chartHightlightMode
). The "newMode" parameter will be one of the following:
"OOLRC_MODE_SUNCOLOR"
"OOLRC_MODE_ECONOMY"
"OOLRC_MODE_GOVERNMENT"
"OOLRC_MODE_TECHLEVEL"
this.chartHightlightModeChanged = function(newMode) { // Your code here }
compassTargetChanged
The compassTargetChanged
handler is called when a new target is selected. Mode can be any of the following:
COMPASS_MODE_BASIC COMPASS_MODE_PLANET COMPASS_MODE_STATION COMPASS_MODE_SUN COMPASS_MODE_TARGET COMPASS_MODE_BEACONS
script example
this.compassTargetChanged = function(whom, mode) { log(' Now targeting ' + whom); // Your code here }
consoleMessageReceived
This method was added in Oolite test release 1.91.
The consoleMessageReceived
handler is called whenever a console message is received. The "text" parameter will contain the text of the message.
this.consoleMessageReceived = function(text) { // Your code here }
dayChanged
This method was added in Oolite test release 1.77.
The dayChanged
handler is called each time a new day starts. At very low frame rates while the clock is updating, it is possible for this handler to be called twice in the same frame. Therefore, clock.days will not be correct for one of the calls. Use the day number (newday parameter) passed to the handler instead.
this.dayChanged = function(newday) { // Your code here }
escapePodSequenceOver
The escapePodSequenceOver
handler is called at the end of the escape pod sequence, after the shipLaunchedEscapePod
event and just prior to the shipWillDockWithStation
and shipDockedWithStation
events.
Use this handler if you need to override the destination for the escape pod with player.setEscapePodDestination()
.
this.escapePodSequenceOver = function() { // Your code here }
guiScreenChanged
The guiScreenChanged
handler is called when the guiScreen
changes. The from parameter is the screen it is leaving and to is the screen initially being switched to. Note that the screen can have changed again in the meantime by the action of other oxps. Therefore, it is generally better to test the global guiScreen
to see which page is really on display instead of using the to parameter. (world script only)
This handler will only fire when switched to/from any of the following:
1.76: "GUI_SCREEN_MAIN"
, "GUI_SCREEN_STATUS"
(F5), "GUI_SCREEN_MANIFEST"
(F5F5), "GUI_SCREEN_SYSTEM_DATA"
(F7), "GUI_SCREEN_OPTIONS"
, "GUI_SCREEN_EQUIP_SHIP"
(F3), "GUI_SCREEN_SHIPYARD"
(F3F3), "GUI_SCREEN_SHORT_RANGE_CHART"
(F6), "GUI_SCREEN_LONG_RANGE_CHART"
(F6F6), "GUI_SCREEN_MARKET"
(F8), "GUI_SCREEN_CONTRACTS"
, "GUI_SCREEN_REPORT"
1.77: adds "GUI_SCREEN_INTERFACES"
(F4) and removes "GUI_SCREEN_CONTRACTS"
1.81: adds "GUI_SCREEN_MARKETINFO"
(F8F8)
this.guiScreenChanged = function(to, from) { // Your code here }
guiScreenWillChange
The guiScreenWillChange
handler is called when the guiScreen
is about to change. On these screens, a script could change the content of the page to be displayed. (world script only)
This handler will only fire when switching to/from any of the following:
"GUI_SCREEN_EQUIP_SHIP"
(F3), "GUI_SCREEN_MANIFEST"
(F5F5), "GUI_SCREEN_MARKET"
(F8), "GUI_SCREEN_SHIPYARD"
(F3F3), "GUI_SCREEN_SYSTEM_DATA"
(F7)
1.77: adds "GUI_SCREEN_INTERFACES"
(F4), "GUI_SCREEN_STATUS"
(F5)
1.81: adds "GUI_SCREEN_MARKETINFO"
(F8F8)
this.guiScreenWillChange = function(to, from) { // Your code here }
infoSystemChanged
This method was added in Oolite test release 1.83.
The infoSystemChanged
handler is called when the system displayed in F7 is changed, e.g. by moving the small blue cursor along the planned route in the chart.
this.infoSystemChanged = function(to, from) { // Your code here }
infoSystemWillChange
This method was added in Oolite test release 1.85.
The infoSystemWillChange
handler is called just before the system displayed in F7 is updated, e.g. by moving the small blue cursor along the planned route in the chart.
this.infoSystemWillChange = function(to, from) { // Your code here }
mfdKeyChanged
This method was added in Oolite test release 1.85.
The mfdKeyChanged
handler is called whenever the player changes the content of an MFD slot. The activeMFD
parameter identifies which MFD slot is being changed, and the mfdKey
specifies the key code now in use in this slot.
this.mfdKeyChanged = function(activeMFD : integer, mfdKey : string) { // Your code here }
See also: PlayerShip.setMultiFunctionDisplay()
missionChoiceWasReset
The missionChoiceWasReset
handler is called when the mission choice is set to null
via script (either using the legacy script method resetMissionChice or using mission.runScreen ({choices: null})
in javascript)
this.missionChoiceWasReset= function() { // Your code here }
missionScreenEnded
The missionScreenEnded
handler is called when a mission screen ends. Note that another script may have put up a new mission screen in the meantime. (world script only)
this.missionScreenEnded = function() { // Your code here }
missionScreenOpportunity
The missionScreenOpportunity
handler is called if there are no mission/report screens active and the player's ship is docked. It gets fired at game startup, upon docking and after an arrival report or previous mission screen has ended. (world script only)
this.missionScreenOpportunity= function() { // Your code here }
This handler works a bit different from other handlers. It fires for every installed OXP/OXZ until one creates a mission screen during this handler, and then it stops. When the mission screen ends and there is no callback function that creates a new mission screen, the missionScreenOpportunity
is sent again to all installed OXPs/OXZs starting with the one that created the last mission screen. All this means is that when this handler fires, it is safe to show a mission screen and no further tests are needed.
reportScreenEnded
The reportScreenEnded
handler is called when the last arrival-report screen ends. This is a screen that should not be written by a missionscreen. The code should wait until this eventhandler fires. Note that an other script may have put up a new missionscreen in the meantime. (world script only)
this.reportScreenEnded = function() { // Your code here }
selectedMFDChanged
This method was added in Oolite test release 1.85.
The selectedMFDChanged
handler is send whenever the player selects a new MFD slot (normally using the default keypress ";"). The activeMFD
paramater is the index of the MFD which is now active.
this.selectedMFDChanged = function(activeMFD : integer) { // Your code here }
shipCollided
The shipCollided
handler is send after a collision with otherShip.
this.shipCollided = function(otherShip) { // Your code here }
shipSpawned
The shipSpawned
handler is called after an NPC ship has been added to the system. After a witchspace jump it means that first all ships are added to the system, then all the relevant shipSpawned events are triggered.
This handler for the worldScript is new since Oolite 1.74. After the event is sent to the shipScript, it is now also send to the worldScript with the added entity as argument.
this.shipSpawned = function(ship) { // Your code here }
shipLaunchedEscapePod
The shipLaunchedEscapePod
handler is called when the player bails out. This will be followed by the escapePodSequenceOver
event, and then a shipWillDockWithStation()
/shipDockedWithStation()
pair after a few seconds.
this.shipLaunchedEscapePod = function(escapepod) { // Your code here }
systemInformationChanged
This method was added in Oolite test release 1.79.
The systemInformationChanged
handler is called when system information is modified. It is passed the galaxy and system ID which were changed, and the key and new value in the SystemInfo object.
To avoid problems with recursion, attempting to change the value of any system information property from within this handler will fail and log an error. Also note that changes which take place while Oolite is not running (from OXP planetinfo.plist files) will not cause this handler to be called when the game is reloaded.
this.systemInformationChanged = function(galaxy,system,key,newValue) { // Your code here }
viewDirectionChanged
The viewDirectionChanged
handler is called when the player view changes, with a string to indicate which view the player is facing. Amongst its possible values are "VIEW_FORWARD", "VIEW_AFT", "VIEW_PORT", "VIEW_STARBOARD",
"VIEW_CUSTOM",
"VIEW_GUI_DISPLAY".
Note: viewDirectionChanged woes (2011)
this.viewDirectionChanged = function(viewString) { if (viewString == "VIEW_PORT") { // Your code here } }
System population
In Oolite 1.79 and later, functions are called to populate and repopulate the system. These functions do not have fixed names, as they depend on the system, but otherwise act like normal worldscript functions. The populator page has more documentation on these functions.
Defunct
equipmentDestroyed
Handler no longer exists in current stable version 1.80
The equipmentDestroyed
handler is called when equipment gets destroyed completely beyond repair. (in strict mode, 1.77 or earlier only) (world script only)
this.equipmentDestroyed = function(equipment) { // Your code here }
tickle
(removed in 1.77)
The tickle
handler is called periodically-ish, whenever the old plist scripts are updated. tickle()
is deprecated. In new code, use appropriate event handlers or timers instead.
Missing Events
All the initially planned events have been implemented in 1.74.
If there are other events you would like to be able to respond to, please write a request on the forum.
See also: ship script event handlers
- Updated JavaScript features in Oolite 1.81
- Updated JavaScript features in Oolite 1.79
- Updated JavaScript features in Oolite 1.85
- Updated JavaScript features in Oolite 1.83
- Updated JavaScript features in Oolite 1.75
- Updated JavaScript features in Oolite 1.77
- Updated JavaScript features in Oolite 1.89
- Updated JavaScript features in Oolite 1.87
- Updated JavaScript features in Oolite 1.91
- Oolite JavaScript Reference