List of Nasal extension functions: Difference between revisions

Jump to navigation Jump to search
m
lang="php" -> lang="nasal"
m (Update forum links)
m (lang="php" -> lang="nasal")
Line 15: Line 15:
As such, the cmdarg() function is primarily used for listener callbacks declared in XML markup, cmdarg() returns the listened-to property as props.Node object, so you can use it with all its methods (see [[$FG_ROOT]]/Nasal/props.nas) for example:
As such, the cmdarg() function is primarily used for listener callbacks declared in XML markup, cmdarg() returns the listened-to property as props.Node object, so you can use it with all its methods (see [[$FG_ROOT]]/Nasal/props.nas) for example:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
print(cmdarg().getPath(), " has been changed to ", cmdarg().getValue())
print(cmdarg().getPath(), " has been changed to ", cmdarg().getValue())
</syntaxhighlight>
</syntaxhighlight>
Line 21: Line 21:
The cmdarg() function avoids that you have to type the exact same path twice (once here and once in the setlistener() command) and it makes clear that this is the listened to property. Also, you can use all the nice props.Node methods on cmdarg() directly:
The cmdarg() function avoids that you have to type the exact same path twice (once here and once in the setlistener() command) and it makes clear that this is the listened to property. Also, you can use all the nice props.Node methods on cmdarg() directly:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
setlistener("/gear/launchbar/state", func {
setlistener("/gear/launchbar/state", func {
     if (cmdarg().getValue() == "Engaged")
     if (cmdarg().getValue() == "Engaged")
Line 62: Line 62:
Concatenates an arbitrary number of arguments to one string, appends a new-line, and prints it to the terminal. Returns the number of printed characters.
Concatenates an arbitrary number of arguments to one string, appends a new-line, and prints it to the terminal. Returns the number of printed characters.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
print("Just", " a ", "test");
print("Just", " a ", "test");
</syntaxhighlight>
</syntaxhighlight>
Line 73: Line 73:
Returns the node value for a given path, or <tt>nil</tt> if the node doesn't exist or hasn't been initialized yet.  
Returns the node value for a given path, or <tt>nil</tt> if the node doesn't exist or hasn't been initialized yet.  


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
getprop(<path>);
getprop(<path>);
</syntaxhighlight>
</syntaxhighlight>
Line 79: Line 79:
Example:
Example:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
print("The frame rate is ", getprop("/sim/frame-rate"), " FPS");
print("The frame rate is ", getprop("/sim/frame-rate"), " FPS");
</syntaxhighlight>
</syntaxhighlight>
Line 87: Line 87:
Sets a property value for a given node path string. Always returns nil.
Sets a property value for a given node path string. Always returns nil.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
setprop(<path> [, <path>, [...]], <value>);
setprop(<path> [, <path>, [...]], <value>);
</syntaxhighlight>
</syntaxhighlight>
Line 95: Line 95:
Note: <tt>setprop()</tt> concatenates a list of input arguments by means of inserting a "/" in between. That is nice for properties, as this slash is part of the tree. However, when one wants to make use of indices, like [0], one has to concatenate by hand (using "~") ''inside'' one part of the string argument list. An example is:
Note: <tt>setprop()</tt> concatenates a list of input arguments by means of inserting a "/" in between. That is nice for properties, as this slash is part of the tree. However, when one wants to make use of indices, like [0], one has to concatenate by hand (using "~") ''inside'' one part of the string argument list. An example is:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var i = 4;
var i = 4;
setprop("instrumentation","cdu","page["~i~"]","title","MENU");
setprop("instrumentation","cdu","page["~i~"]","title","MENU");
Line 104: Line 104:
Examples:
Examples:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
setprop("/sim/current-view/view-number", 2);
setprop("/sim/current-view/view-number", 2);
setprop("/controls", "engines/engine[0]", "reverser", 1);
setprop("/controls", "engines/engine[0]", "reverser", 1);
Line 111: Line 111:
'''Erasing a property from the property tree''': a property that has been created, for example through <tt>setprop()</tt> can be erased via
'''Erasing a property from the property tree''': a property that has been created, for example through <tt>setprop()</tt> can be erased via


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
props.globals.getNode("foo/bar").remove(); # take out the complete node
props.globals.getNode("foo/bar").remove(); # take out the complete node
props.globals.getNode("/foo").removeChild("bar"); # take out a certain child node
props.globals.getNode("/foo").removeChild("bar"); # take out a certain child node
Line 119: Line 119:
Give the value from a value or a source node to a destination node in given time.
Give the value from a value or a source node to a destination node in given time.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
interpolate(<path>, <value>, <time>);
interpolate(<path>, <value>, <time>);


Examples:
Examples:
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
interpolate("controls/switches/nav-lights-pos", 1, 0.25); # After 25ms, nav-lights-pos = 1
interpolate("controls/switches/nav-lights-pos", 1, 0.25); # After 25ms, nav-lights-pos = 1
interpolate("controls/gear/brake-left-pos", getprop("controls/gear/brake-left"), 1); # After 1s, brake-left-pos = brake-left
interpolate("controls/gear/brake-left-pos", getprop("controls/gear/brake-left"), 1); # After 1s, brake-left-pos = brake-left
Line 131: Line 131:
Runs a function after a given simulation time (default) or real time in seconds.
Runs a function after a given simulation time (default) or real time in seconds.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
settimer(<function>, <time> [, <realtime=0>]);
settimer(<function>, <time> [, <realtime=0>]);
</syntaxhighlight>
</syntaxhighlight>
Line 137: Line 137:
The first object is a function object (ie, "func { ... }").  Note that this is different from a function call (ie, "func ( ... )"). If you don't understand what this means, just remember to always enclose the first argument in any call to settimer with the word "func" and braces "{ }", and it will always work. For instance, if you want print the words "My result" in five seconds, use this code:
The first object is a function object (ie, "func { ... }").  Note that this is different from a function call (ie, "func ( ... )"). If you don't understand what this means, just remember to always enclose the first argument in any call to settimer with the word "func" and braces "{ }", and it will always work. For instance, if you want print the words "My result" in five seconds, use this code:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
settimer ( func { print ( "My result"); }, 5);
settimer ( func { print ( "My result"); }, 5);
</syntaxhighlight>
</syntaxhighlight>
Line 143: Line 143:
Inside the braces of the func object you can put any valid Nasal code, including a function call.  In fact, if you want to call a function with certain values as arguments, the way to do it is to turn it into a function object by enclosing it with a func{}, for example:
Inside the braces of the func object you can put any valid Nasal code, including a function call.  In fact, if you want to call a function with certain values as arguments, the way to do it is to turn it into a function object by enclosing it with a func{}, for example:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
myarg1="My favorite string";
myarg1="My favorite string";
myarg2=432;
myarg2=432;
Line 151: Line 151:
The third argument is optional and defaults to 0, which lets the time argument be interpreted as "seconds simulation time". In this case the timer doesn't run when FlightGear is paused. For user interaction purposes (measuring key press time, displaying popups, etc.) one usually prefers real time.
The third argument is optional and defaults to 0, which lets the time argument be interpreted as "seconds simulation time". In this case the timer doesn't run when FlightGear is paused. For user interaction purposes (measuring key press time, displaying popups, etc.) one usually prefers real time.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
# simulation time example
# simulation time example
var copilot_annoyed = func { setprop("/sim/messages/copilot", "Stop it! Immediately!") }
var copilot_annoyed = func { setprop("/sim/messages/copilot", "Stop it! Immediately!") }
Line 157: Line 157:
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
# real time example
# real time example
var popdown = func ( tipArg ) {  
var popdown = func ( tipArg ) {  
Line 189: Line 189:
As of 2.11, there is a new API for making a timer that allows more control over what happens in a timer – as opposed to setting one and forgetting about it.
As of 2.11, there is a new API for making a timer that allows more control over what happens in a timer – as opposed to setting one and forgetting about it.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var timer = maketimer(<interval>, <function>)
var timer = maketimer(<interval>, <function>)
var timer = maketimer(<interval>, <self>, <function>)
var timer = maketimer(<interval>, <self>, <function>)
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
timer.start()
timer.start()
timer.stop()
timer.stop()
Line 202: Line 202:
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
# create timer with 1 second interval
# create timer with 1 second interval
var timer = maketimer(1.0, func { print('timer called'); });
var timer = maketimer(1.0, func { print('timer called'); });
Line 215: Line 215:
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var Tooltip = {
var Tooltip = {
   new: func
   new: func
Line 242: Line 242:
Returns information about geodetic coordinates. Takes two arguments: lat, lon (in degree) and returns a vector with two entries, or nil if no information could be obtained because the terrain tile wasn't loaded. The first entry is the elevation (in meters) for the given point, and the second is a hash with information about the assigned material, or nil if there was no material information available, because there is, for instance, an untextured building at that spot or the scenery tile is not loaded.
Returns information about geodetic coordinates. Takes two arguments: lat, lon (in degree) and returns a vector with two entries, or nil if no information could be obtained because the terrain tile wasn't loaded. The first entry is the elevation (in meters) for the given point, and the second is a hash with information about the assigned material, or nil if there was no material information available, because there is, for instance, an untextured building at that spot or the scenery tile is not loaded.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var lat = getprop("/position/latitude-deg");
var lat = getprop("/position/latitude-deg");
var lon = getprop("/position/longitude-deg");
var lon = getprop("/position/longitude-deg");
Line 256: Line 256:
A full data set looks like this:
A full data set looks like this:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
debug.dump(geodinfo(lat, lon));
debug.dump(geodinfo(lat, lon));


Line 271: Line 271:
Usage:
Usage:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var apt = airportinfo("KHAF");  # get info about KHAF
var apt = airportinfo("KHAF");  # get info about KHAF
var apt = airportinfo(lat, lon); # get info about apt closest to lat/lon
var apt = airportinfo(lat, lon); # get info about apt closest to lat/lon
Line 279: Line 279:
The command debug.dump(airportinfo("KHAF")) outputs this:
The command debug.dump(airportinfo("KHAF")) outputs this:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
{ lon : -122.4962626410256, lat : 37.51343502564102, has_metar : 0,
{ lon : -122.4962626410256, lat : 37.51343502564102, has_metar : 0,
   runways : { 12 : { stopway2 : 0, threshold1 : 232.5624,
   runways : { 12 : { stopway2 : 0, threshold1 : 232.5624,
Line 335: Line 335:


Usage:
Usage:
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var fp = flightplan();
var fp = flightplan();
var fp = flightplan('/some/path/to/a/flightplan.xml');
var fp = flightplan('/some/path/to/a/flightplan.xml');
Line 343: Line 343:




<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var fp = flightplan();
var fp = flightplan();
for (var i=0; i<fp.getPlanSize(); i += 1)
for (var i=0; i<fp.getPlanSize(); i += 1)
Line 355: Line 355:
Returns epoch time (time since 1972/01/01 00:00) in seconds as a floating point number with high resolution. This is useful for benchmarking purposes.
Returns epoch time (time since 1972/01/01 00:00) in seconds as a floating point number with high resolution. This is useful for benchmarking purposes.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
#benchmarking example:
#benchmarking example:
var start = systime();
var start = systime();
Line 366: Line 366:
Converts cartesian coordinates x/y/z to geodetic coordinates lat/lon/alt, which are returned as a vector. Units are degree and meter.
Converts cartesian coordinates x/y/z to geodetic coordinates lat/lon/alt, which are returned as a vector. Units are degree and meter.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var geod = carttogeod(-2737504, -4264101, 3862172);
var geod = carttogeod(-2737504, -4264101, 3862172);
print("lat=", geod[0], " lon=", geod[1], " alt=", geod[2]);
print("lat=", geod[0], " lon=", geod[1], " alt=", geod[2]);
Line 377: Line 377:
Converts geodetic coordinates lat/lon/alt to cartesian coordinates x/y/z. Units are degree and meter.
Converts geodetic coordinates lat/lon/alt to cartesian coordinates x/y/z. Units are degree and meter.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var cart = geodtocart(37.5, -122.7, 1000); # lat/lon/alt(m)
var cart = geodtocart(37.5, -122.7, 1000); # lat/lon/alt(m)
print("x=", cart[0], " y=", cart[1], " z=", cart[2]);
print("x=", cart[0], " y=", cart[1], " z=", cart[2]);
Line 389: Line 389:
This function is an interface to the built-in [http://expat.sourceforge.net/ Expat XML parser]. It takes up to five arguments. The first is a mandatory absolute path to an XML file, the remaining four are optional callback functions, each of which can be nil (which is also the default value).
This function is an interface to the built-in [http://expat.sourceforge.net/ Expat XML parser]. It takes up to five arguments. The first is a mandatory absolute path to an XML file, the remaining four are optional callback functions, each of which can be nil (which is also the default value).


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var ret = parsexml(<path> [, <start-elem> [, <end-elem> [, <data> [, <pi> ]]]]);
var ret = parsexml(<path> [, <start-elem> [, <end-elem> [, <data> [, <pi> ]]]]);


Line 402: Line 402:
Example:
Example:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var start = func(name, attr) {
var start = func(name, attr) {
     print("starting tag ", name);
     print("starting tag ", name);
Line 420: Line 420:
Example:
Example:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var guess_path = func(path...) {
var guess_path = func(path...) {
     var path_concat = string.join(path, "/");
     var path_concat = string.join(path, "/");
395

edits

Navigation menu