Difference between revisions of "Oolite JavaScript Reference: Global"

From Elite Wiki
(Update for 1.75.)
(Cleanup.)
Line 9: Line 9:
 
{{oolite-prop-added|1.75|beta}}
 
{{oolite-prop-added|1.75|beta}}
 
  '''defaultFont''' : Object (read-only)
 
  '''defaultFont''' : Object (read-only)
An object with a single method, <code>measureString(string)</code>, which returns the width of the specified string, as it would be shown on the screen, in '''ems'''. One em is the intrinsic unit size of a font; for example, if a font is rendered at 12 points, 1 em == 12 pt. The mission screen text area is 32 em wide.
+
An object with a single method, <code>measureString(string)</code>, which returns the width of the specified string, as it would be shown on the screen, in '''ems'''. One em is the intrinsic unit size of a font; for example, if a font is rendered at 12 points, 1 em = 12 pt. The mission screen text area is 32 em wide.
  
 
=== <code>galaxyNumber</code> ===
 
=== <code>galaxyNumber</code> ===
Line 21: Line 21:
 
=== <code>guiScreen</code> ===
 
=== <code>guiScreen</code> ===
 
  '''guiScreen''' : String (read-only)
 
  '''guiScreen''' : String (read-only)
Returns the screen the player is looking at. If in flight, it's set to "GUI_SCREEN_MAIN".
+
Returns the screen the player is looking at. If in flight, this is <code>"GUI_SCREEN_MAIN"</code>. Currently, the other possible values are <code>"GUI_SCREEN_INTRO1"</code>, <code>"GUI_SCREEN_INTRO2"</code>, <code>"GUI_SCREEN_STATUS"</code>, <code>"GUI_SCREEN_MANIFEST"</code>, <code>"GUI_SCREEN_EQUIP_SHIP"</code>, <code>"GUI_SCREEN_SHIPYARD"</code>, <code>"GUI_SCREEN_LONG_RANGE_CHART"</code>, <code>"GUI_SCREEN_SHORT_RANGE_CHART"</code>, <code>"GUI_SCREEN_SYSTEM_DATA"</code>, <code>"GUI_SCREEN_MARKET"</code>, <code>"GUI_SCREEN_CONTRACTS"</code>, <code>"GUI_SCREEN_OPTIONS"</code>, <code>"GUI_SCREEN_GAMEOPTIONS"</code>, <code>"GUI_SCREEN_LOAD"</code>, <code>"GUI_SCREEN_SAVE"</code>, <code>"GUI_SCREEN_SAVE_OVERWRITE"</code>, <code>"GUI_SCREEN_STICKMAPPER"</code>, <code>"GUI_SCREEN_MISSION"</code> and <code>"GUI_SCREEN_REPORT"</code>. This list may change in future.
 +
 
  
 
=== <code>manifest</code> ===
 
=== <code>manifest</code> ===
  '''manifest''' : Object (read-only)
+
  '''manifest''' : {{oojsclass|Manifest}} (read-only)
This property allows an OXP to directly access the player ship's manifest properties.
+
An alias to <code>[[Oolite JavaScript Reference: PlayerShip#manifest|player.ship.manifest]]</code>.
  
 
=== <code>mission</code> ===
 
=== <code>mission</code> ===
  '''mission''' : Object (read-only)
+
  '''mission''' : {{oojsclass|Mission}} (read-only)
This property allows an OXP to access misson methods.
+
See [[Oolite JavaScript Reference: Mission]].
  
 
=== <code>missionVariables</code> ===
 
=== <code>missionVariables</code> ===
Line 51: Line 52:
  
 
=== <code>oolite</code> ===
 
=== <code>oolite</code> ===
  '''oolite''' : Object (read-only)
+
  '''oolite''' : {{oojsclass|Oolite}} (read-only)
This property allows an OXP to access Oolite's properties and methods.
+
An object describing properties of the Oolite application; see [[Oolite JavaScript Reference: Oolite]].
  
 
=== <code>player</code> ===
 
=== <code>player</code> ===
  '''player''' : Object (read-only)
+
  '''player''' : {{oojsclass|Player}} (read-only)
 
This property allows an OXP to access the player object's properties and methods.
 
This property allows an OXP to access the player object's properties and methods.
  
 
=== <code>system</code> ===
 
=== <code>system</code> ===
  '''system''' : Object (read-only)
+
  '''system''' : {{oojsclass|System}} (read-only)
 
This property allows an OXP to access the current system's properties and methods.
 
This property allows an OXP to access the current system's properties and methods.
  
 
=== <code>timeAccelerationFactor</code> ===
 
=== <code>timeAccelerationFactor</code> ===
 
  '''timeAccelerationFactor''' : Number (read/write)
 
  '''timeAccelerationFactor''' : Number (read/write)
Also known as TAF. Will change the ratio between game real time and actual real time (default is 1). The accepted range is between 0.0625 (1/16) and 16. Attempting to set numbers outside this range will reset the TAF to 1.
+
Also known as TAF. Will change the ratio between game real time and actual real time (default is 1). The accepted range is between 0.0625 (1/16) and 16. Attempting to set numbers outside this range will reset the TAF to 1. (In end-user stable builds, this will be a read-only property with the value 1.)
  
 
=== <code>worldScripts</code> ===
 
=== <code>worldScripts</code> ===
 
  '''worldScripts''' : Object (read-only)
 
  '''worldScripts''' : Object (read-only)
This property allows an OXP to access all world scripts, their properties and functions.
+
A list of world script objects.
  
 
'''Example:'''
 
'''Example:'''
  log(worldScripts['oolite-nova'].version);
+
  log(worldScripts["oolite-nova"].version);
  
 
== Methods ==
 
== Methods ==
Line 98: Line 99:
 
  function '''expandDescription'''(description : String [, locals : Object (Dictionary)]) : String
 
  function '''expandDescription'''(description : String [, locals : Object (Dictionary)]) : String
 
Expands a string, substituting special tokens and any key inside square brackets with the substitution rules for [[Methods#Communications|Communications]].
 
Expands a string, substituting special tokens and any key inside square brackets with the substitution rules for [[Methods#Communications|Communications]].
When local key/value pairs are provided, they take precedence over any other values. (buggy at the time of writing, fixed for 1.74.2 and later versions.)
+
When local key/value pairs are provided, they take precedence over any other values.
  
 
'''Example:'''
 
'''Example:'''
  expandDescription('My ball is [my_color].', { 'my_color': 'red' });  
+
  expandDescription("My ball is [my_color].", { my_color: "red" });  
  
 
=== <code>expandMissionText</code>  ===
 
=== <code>expandMissionText</code>  ===
Line 108: Line 109:
  
 
'''Example:'''
 
'''Example:'''
  expandMissionText('oolite_trumble_title');
+
  expandMissionText("oolite_trumble_title");
  
 
=== <code>isValidFrameCallback</code> ===
 
=== <code>isValidFrameCallback</code> ===
 
{{oolite-method-added|1.75|beta}}
 
{{oolite-method-added|1.75|beta}}
  function isValidFrameCallback(trackingID: ''TrackingID'') : boolean
+
  function isValidFrameCallback(trackingID: ''TrackingID'') : Boolean
Returns <code>true</code> if <code>trackingID</code> identifies a frame callback which has been registered with <code>[[#addFrameCallback|addFrameCallback()]]</code> and not yet removed.
+
Returns <code>true</code> if <code>trackingID</code> identifies a frame callback which has been registered with <code>[[#addFrameCallback|addFrameCallback()]]</code> and not yet removed. (There should be no need to use this except for debugging and testing.)
  
 
'''See also:''' <code>[[#addFrameCallback|addFrameCallback()]]</code>, <code>[[#removeFrameCallback|removeFrameCallback()]]</code>
 
'''See also:''' <code>[[#addFrameCallback|addFrameCallback()]]</code>, <code>[[#removeFrameCallback|removeFrameCallback()]]</code>
Line 122: Line 123:
  
 
'''Example:'''
 
'''Example:'''
  log('myOXP.init','MyOXP initialised and ready!');
+
  log("myOXP.init","MyOXP initialised and ready!");
  
 
=== <code>randomInhabitantsDescription</code>  ===
 
=== <code>randomInhabitantsDescription</code>  ===
Line 130: Line 131:
 
'''Example:'''
 
'''Example:'''
 
  player.consoleMessage("You'll meet a " +  randomInhabitantsDescription() +
 
  player.consoleMessage("You'll meet a " +  randomInhabitantsDescription() +
                       ". She'll be with a group of " + randomInhabitantsDescription(true) +'.');
+
                       ". She'll be with a group of " + randomInhabitantsDescription(true) +".");
  
 
=== <code>randomName</code>  ===
 
=== <code>randomName</code>  ===
Line 137: Line 138:
  
 
'''Example:'''
 
'''Example:'''
  player.consoleMessage(randomName() + ' ' + randomName() +' rules this system with an iron fist.');
+
  player.consoleMessage(randomName() + " " + randomName() +" rules this system with an iron fist.");
  
 
=== <code>removeFrameCallback</code> ===
 
=== <code>removeFrameCallback</code> ===
Line 151: Line 152:
  
 
In Oolite 1.75 and later, ''guiTextureSpecifier'' can be either a string or an object with the required key <code>name</code> and optional keys <code>width</code> and <code>height</code>. If neither width nor height is specified, the dimensions of the image are used. If only one is specified, the other is calculated such that the image is not distorted. The scale unit is 1/480 of the height of the window; in other words, the window is always considered 480 units high, and the width depends on the aspect ratio of the window.
 
In Oolite 1.75 and later, ''guiTextureSpecifier'' can be either a string or an object with the required key <code>name</code> and optional keys <code>width</code> and <code>height</code>. If neither width nor height is specified, the dimensions of the image are used. If only one is specified, the other is calculated such that the image is not distorted. The scale unit is 1/480 of the height of the window; in other words, the window is always considered 480 units high, and the width depends on the aspect ratio of the window.
 
In earlier test releases, only a string is accepted and the width and height are always those of the texture after loading.
 
  
 
'''Example:'''
 
'''Example:'''

Revision as of 11:04, 20 February 2011

Global variables and functions are visible to all scripts. They are also properties of global, which is itself a global variable and hence a property of itself.

Properties

clock

clock : Clock (read-only)

This property allows an OXP to access the game clock's properties and methods.

defaultFont

This property was added in Oolite beta release 1.75.

defaultFont : Object (read-only)

An object with a single method, measureString(string), which returns the width of the specified string, as it would be shown on the screen, in ems. One em is the intrinsic unit size of a font; for example, if a font is rendered at 12 points, 1 em = 12 pt. The mission screen text area is 32 em wide.

galaxyNumber

galaxyNumber : Number (read-only)

Returns the number ot the galaxy the player is in.

global

global : Global (read-only)

All global variables and functions are actually properties of the global object. The global object is a property of itself, and this is it.

guiScreen

guiScreen : String (read-only)

Returns the screen the player is looking at. If in flight, this is "GUI_SCREEN_MAIN". Currently, the other possible values are "GUI_SCREEN_INTRO1", "GUI_SCREEN_INTRO2", "GUI_SCREEN_STATUS", "GUI_SCREEN_MANIFEST", "GUI_SCREEN_EQUIP_SHIP", "GUI_SCREEN_SHIPYARD", "GUI_SCREEN_LONG_RANGE_CHART", "GUI_SCREEN_SHORT_RANGE_CHART", "GUI_SCREEN_SYSTEM_DATA", "GUI_SCREEN_MARKET", "GUI_SCREEN_CONTRACTS", "GUI_SCREEN_OPTIONS", "GUI_SCREEN_GAMEOPTIONS", "GUI_SCREEN_LOAD", "GUI_SCREEN_SAVE", "GUI_SCREEN_SAVE_OVERWRITE", "GUI_SCREEN_STICKMAPPER", "GUI_SCREEN_MISSION" and "GUI_SCREEN_REPORT". This list may change in future.


manifest

manifest : Manifest (read-only)

An alias to player.ship.manifest.

mission

mission : Mission (read-only)

See Oolite JavaScript Reference: Mission.

missionVariables

missionVariables : Object (read-only)

The missionVariables object stores values that are stored in saved games. This is the main way for scripts to store information permanently.

Whenever a value is assigned to a property of missionVariables, it is converted to a string object and tracked with the player. When the value is read, it will be converted to a number if possible, otherwise returned as a string. Example:

missionVariables.exampleVar = 42;
let x = missionVariables.exampleVar; // x is now the number 42

Mission variables can also be used in legacy scripts and in descriptions.plist string expansions, by prefixing the name with “mission_”. Example:

expandDescription("My variable is [mission_exampleVar].")

Setting and retrieving mission variables has a small but non-trivial overhead. If a function needs to use a mission variable value several times, it is strongly recommended that it be copied into a local variable and, if necessary, stored back once. Example:

let x = missionVariables.exampleVar;
if (x < 3)  x = processSmallValue(x);
else  x = processBigValue(x);
missionVariables.exampleVar = x;

Mission variable names may not begin with an underscore (“_”). Unassigned mission variables are always null.

oolite

oolite : Oolite (read-only)

An object describing properties of the Oolite application; see Oolite JavaScript Reference: Oolite.

player

player : Player (read-only)

This property allows an OXP to access the player object's properties and methods.

system

system : System (read-only)

This property allows an OXP to access the current system's properties and methods.

timeAccelerationFactor

timeAccelerationFactor : Number (read/write)

Also known as TAF. Will change the ratio between game real time and actual real time (default is 1). The accepted range is between 0.0625 (1/16) and 16. Attempting to set numbers outside this range will reset the TAF to 1. (In end-user stable builds, this will be a read-only property with the value 1.)

worldScripts

worldScripts : Object (read-only)

A list of world script objects.

Example:

log(worldScripts["oolite-nova"].version);

Methods

addFrameCallback

This method was added in Oolite beta release 1.75.

function addFrameCallback(callback : Function) : TrackingID

Registers a frame callback which will be invoked every frame, after entity updates but before rendering. This can be used to implement animations. The return value is a tracking ID which may be passed to removeFrameCallback(); the type and meaning of the tracking ID is unspecified and may change at any time. Frame callbacks are automatically removed when the game resets (for instance, when the player dies), but not at any other time.

The callback is passed one parameter, delta, which is the time since the last frame (in game clock time). Frame callbacks fire even when the game is paused, in which case delta is 0.

Example:

var sum = 0;
this.fcb = addFrameCallback(function (delta)
{
    sum += delta;
    log("Time elapsed: " + sum + " (delta: " + delta + ")");
}

See also: removeFrameCallback(), isValidFrameCallback()

displayNameForCommodity

function displayNameForCommodity(commodityName : String) 

Returns the display name corresponding to the specified commodity. Useful in conjunction with localisation OXPs, or expansions that rename commodities depending on which station / system the player is at.

expandDescription

function expandDescription(description : String [, locals : Object (Dictionary)]) : String

Expands a string, substituting special tokens and any key inside square brackets with the substitution rules for Communications. When local key/value pairs are provided, they take precedence over any other values.

Example:

expandDescription("My ball is [my_color].", { my_color: "red" }); 

expandMissionText

function expandMissionText(textKey: String [, locals : Object (Dictionary)]) : String

Load a string specified by textKey from missiontext.plist, then perform expandDescription()-type substitutions on it, and also replace “\n” with line breaks. The local parameter works as with expandDescription().

Example:

expandMissionText("oolite_trumble_title");

isValidFrameCallback

This method was added in Oolite beta release 1.75.

function isValidFrameCallback(trackingID: TrackingID) : Boolean

Returns true if trackingID identifies a frame callback which has been registered with addFrameCallback() and not yet removed. (There should be no need to use this except for debugging and testing.)

See also: addFrameCallback(), removeFrameCallback()

log

function log([messageClass : String ,] message : String)

Writes a message to Oolite's log. The optional messageClass can be used to specify which type of message is written to the log.

Example:

log("myOXP.init","MyOXP initialised and ready!");

randomInhabitantsDescription

function randomInhabitantsDescription([plural : boolean])

Returns a random capitalised word, suitable to describe one sentient being, or - if using the optional plural argument - a group of sentients.

Example:

player.consoleMessage("You'll meet a " +  randomInhabitantsDescription() +
                     ". She'll be with a group of " + randomInhabitantsDescription(true) +".");

randomName

function randomName()

Returns a random capitalised word, suitable for use as name, or indeed surname.

Example:

player.consoleMessage(randomName() + " " + randomName() +" rules this system with an iron fist.");

removeFrameCallback

This method was added in Oolite beta release 1.75.

function removeFrameCallback(trackingID : TrackingID)

Removes a frame callback previously registered with addFrameCallback().

See also: addFrameCallback(), isValidFrameCallback()

setScreenBackground

function setScreenBackground(image : guiTextureSpecifier) 

Temporary override that sets the background of the current gui screen to the specified image. Override is lost after the player switches screens.

In Oolite 1.75 and later, guiTextureSpecifier can be either a string or an object with the required key name and optional keys width and height. If neither width nor height is specified, the dimensions of the image are used. If only one is specified, the other is calculated such that the image is not distorted. The scale unit is 1/480 of the height of the window; in other words, the window is always considered 480 units high, and the width depends on the aspect ratio of the window.

Example:

setScreenBackground({ name: "oolite-nova-system.png", height: 512 });

setScreenOverlay

function setScreenOverlay(image : guiTextureSpecifier) 

Temporary override that sets the overlay of the current gui screen to the specified image. Override is lost after the player switches screens.

See setScreenBackground() for a discussion of guiTextureSpecifier.

Example:

setScreenOverlay('trumblebox.png');

takeSnapShot

This method was added in Oolite beta release 1.75.

function takeSnapShot([filename : String]) : Bool

Saves a snapshot of the current screen to your hard disk. A filename is optional. When a name is used, only alphanumeric values and "-" and "_" are allowed for the filename. The appropriate extension for the used operating system (like .png) is added by Oolite and should not be part of the filename.
When a filename already exists, it is not overwritten but the string "-xxx" is added, starting with "-001".
takeSnapShot() will not be available in the stable release version of Oolite, but will be available in a special OXP developer release.

Example:

takeSnapShot('mission_target_1');