List of Nasal extension functions: Difference between revisions

Jump to navigation Jump to search
m
Cleanup, minor additions, reordering
mNo edit summary
m (Cleanup, minor additions, reordering)
Line 1: Line 1:
{{Incomplete|The accuracy of this article or section may be compromised due to recent changes in the source code, please help us update this article by incorporating the [http://mapserver.flightgear.org/git/?p=flightgear;a=blob;f=src/Scripting/NasalSys.cxx#l707 latest extension functions]}}
{{Incomplete|The accuracy of this article or section may be compromised due to recent changes in the source code, please help us update this article by incorporating the [http://mapserver.flightgear.org/git/?p=flightgear;a=blob;f=src/Scripting/NasalSys.cxx#l707 latest extension functions]}}
{{Template:Nasal Navigation}}
{{Template:Nasal Navigation}}
== Nasal extension functions ==
For a list of function coded in C that are available to Nasal implementations in general, see [http://plausible.org/nasal/lib.html]. Note that only the <tt>thread</tt>, <tt>io</tt>, <tt>math</tt>, and <tt>utf8</tt> libraries are currently included in SimGear.


== FlightGear extension functions ==
== FlightGear extension functions ==
Line 50: Line 54:


As you can guess, there's also a removecommand() function which will remove '''any''' command – even those implemented in C++! As such it can be very dangerous and remove core functionality, so use with caution.
As you can guess, there's also a removecommand() function which will remove '''any''' command – even those implemented in C++! As such it can be very dangerous and remove core functionality, so use with caution.
=== <tt>logprint()</tt> ===
The first argument should be a number specifying the priority (i.e. matching a sgDebugPriority: 0 for warn, etc.) and the rest of the arguments get concatenated to form the message for sglog.log() (basically the builtin logging mechanism). Returns the length of the message in bytes.


=== <tt>print()</tt> ===
=== <tt>print()</tt> ===
Line 60: Line 60:
  print("Just", " a ", "test");
  print("Just", " a ", "test");


=== <tt>logprint()</tt> ===


Similar, but the first argument is reserved for number specifying the priority (i.e. matching a sgDebugPriority object: 1 for bulk, 2 for debug, 3 for info, and 4 for warn).


=== <tt>getprop()</tt> ===
=== <tt>getprop()</tt> ===
Line 152: Line 154:
  }
  }


[[Nasal scripting language#settimer loops|More information about best practices for using the settimer function to create loops in Nasal is elsewhere on this page.]]
[[Nasal scripting language#settimer loops|More information about using the settimer function to create loops.]]


=== <tt>maketimer()</tt> (v. 2.11+) ===
=== <tt>maketimer()</tt> (v. 2.11+) ===
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="php">
Line 205: Line 209:
};
};
</syntaxhighlight>
</syntaxhighlight>
=== <tt>systime()</tt> ===
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.
  #benchmarking example:
  var start = systime();
  how_fast_am_I(123);
  var end = systime();
  print("took ", end - start, " seconds");
=== <tt>carttogeod()</tt> ===
Converts cartesian coordinates x/y/z to geodetic coordinates lat/lon/alt, which are returned as a vector. Units are degree and meter.
var geod = carttogeod(-2737504, -4264101, 3862172);
print("lat=", geod[0], " lon=", geod[1], " alt=", geod[2]);
# outputs
lat=37.49999782141546 lon=-122.6999914632327 alt=998.6042055172776
=== <tt>geodtocart()</tt> ===
Converts geodetic coordinates lat/lon/alt to cartesian coordinates x/y/z. Units are degree and meter.
var cart = geodtocart(37.5, -122.7, 1000); # lat/lon/alt(m)
print("x=", cart[0], " y=", cart[1], " z=", cart[2]);
# outputs
x=-2737504.667684828 y=-4264101.900993474 z=3862172.834656495


=== <tt>geodinfo()</tt> ===
=== <tt>geodinfo()</tt> ===
Line 260: Line 233:


Note that geodinfo is a *very* CPU intensive operation, particularly in FG 2.4.0 and earlier, so use sparingly ([http://flightgear.org/forums/viewtopic.php?f=4&p=135044#p135044 forum discussion here]).
Note that geodinfo is a *very* CPU intensive operation, particularly in FG 2.4.0 and earlier, so use sparingly ([http://flightgear.org/forums/viewtopic.php?f=4&p=135044#p135044 forum discussion here]).
=== <tt>parsexml()</tt> ===
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).
var ret = parsexml(<path> [, <start-elem> [, <end-elem> [, <data> [, <pi> ]]]]);
<start-elem>  ... called for every starting tag with two arguments: the tag name, and an attribute hash
<end-elem>    ... called for every ending tag with one argument: the tag name
<data>        ... called for every piece of data with one argument: the data string
<pi>          ... called for every "processing information" with two args: target and data string
<ret>        ... the return value is nil on error, and the <path> otherwise
Example:
var start = func(name, attr) {
    print("starting tag ", name);
    foreach (var a; keys(attr))
        print("\twith attribute ", a, "=", attr[a]);
}
var end = func(name) { print("ending tag ", name) }
var data = func(data) { print("data=", data) }
var pi = func(target, data) { print("processing instruction: target=", target, " data=", data) }
parsexml("/tmp/foo.xml", start, end, data, pi);


=== airportinfo() ===
=== airportinfo() ===
Line 353: Line 301:
  var fp = flightplan('/some/path/to/a/flightplan.xml');
  var fp = flightplan('/some/path/to/a/flightplan.xml');


In advance of converting the Map and NavDisplay to use the [[Canvas]], James has improved the "flightplan()" extension function of the [[Nasal]] scripting interpreter to expose the full route-path vector for each flight plan leg, as a vector on the leg .
In advance of converting the Map and NavDisplay to use the [[Canvas]], James has improved the "flightplan()" extension function of the [[Nasal]] scripting interpreter to expose the full route-path vector for each flight plan leg as a vector on the leg.




Line 364: Line 312:
}
}
</syntaxhighlight>
</syntaxhighlight>
=== <tt>systime()</tt> ===
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.
  #benchmarking example:
  var start = systime();
  how_fast_am_I(123);
  var end = systime();
  print("took ", end - start, " seconds");
=== <tt>carttogeod()</tt> ===
Converts cartesian coordinates x/y/z to geodetic coordinates lat/lon/alt, which are returned as a vector. Units are degree and meter.
var geod = carttogeod(-2737504, -4264101, 3862172);
print("lat=", geod[0], " lon=", geod[1], " alt=", geod[2]);
# outputs
lat=37.49999782141546 lon=-122.6999914632327 alt=998.6042055172776
=== <tt>geodtocart()</tt> ===
Converts geodetic coordinates lat/lon/alt to cartesian coordinates x/y/z. Units are degree and meter.
var cart = geodtocart(37.5, -122.7, 1000); # lat/lon/alt(m)
print("x=", cart[0], " y=", cart[1], " z=", cart[2]);
# outputs
x=-2737504.667684828 y=-4264101.900993474 z=3862172.834656495
=== <tt>parsexml()</tt> ===
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).
<pre>
var ret = parsexml(<path> [, <start-elem> [, <end-elem> [, <data> [, <pi> ]]]]);
<start-elem>  ... called for every starting tag with two arguments: the tag name, and an attribute hash
<end-elem>    ... called for every ending tag with one argument: the tag name
<data>        ... called for every piece of data with one argument: the data string
<pi>          ... called for every "processing information" with two args: target and data string
<ret>        ... the return value is nil on error, and the <path> otherwise
</pre>
Example:
<syntaxhighlight lang="php">
var start = func(name, attr) {
    print("starting tag ", name);
    foreach (var a; keys(attr))
        print("\twith attribute ", a, "=", attr[a]);
}
var end = func(name) { print("ending tag ", name) }
var data = func(data) { print("data=", data) }
var pi = func(target, data) { print("processing instruction: target=", target, " data=", data) }
parsexml("/tmp/foo.xml", start, end, data, pi);
</syntaxhighlight>
=== <tt>rand()</tt> ===
Return a random number as generated by <tt>sg_random</tt>.
=== <tt>srand()</tt> ===
Seed the random number generator based upon the current time. Returns 0.


=== <tt>abort()</tt> ===
=== <tt>abort()</tt> ===


Wrapper for the C++ library abort() function.
Wrapper for the C++ library abort() function – i.e. it just aborts the process without regard to what's happening. For exiting (gracefully) from FlightGear use the fgcommand "exit" instead.


<references/>
<references/>
395

edits

Navigation menu