Nasal scripting language: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
No edit summary
Line 5: Line 5:
{{WIP}}
{{WIP}}
{{Template:Nasal Navigation}}
{{Template:Nasal Navigation}}
==Built-in functions==
===sort(vector, function)===
Creates a new vector containing the elements in the input vector sorted in ascending order according to the rule given by function, which takes two arguments (elements of the input vector) and should return less than zero, zero, or greater than zero if the first argument is, respectively, less than, equal to, or greater than the second argument. Despite being implemented with ANSI C qsort(), the sort is stable; "equal" elements in the output vector will appear in the same relative order as they do in the input.
Because you can define the sort function, sort allows you to create a list of keys sorting a hash by any criterion--by key, value, or (if, for instance the hash values are hashes themselves) any subvalue.
vec = [100,24,45];
sortvec = sort (vec, func (a,b) cmp (a,b));
debug.dump (sortvec); #output is [24,45,100]
Here is an example of how to output the contents of a hash in sorted order.  Note that the function does not actually sort the hash but returns a list of the hash keys in sorted order.
var airport = {
  "LOXZ": "Zeltweg",
  "LOWI": "Innsbruck",
  "LOXL": "Linz Hoersching",    # the last comma is optional
};
var sortedkeys= sort (keys(airport), func (a,b) cmp (airport[a], airport[b]));
foreach (var i; sortedkeys)
  print (i, ": ", airport[i]);
The output is:
  LOWI: Innsbruck
  LOXL: Linz Hoersching
  LOXZ: Zeltweg 
If the hash values are themselves hashes, sorting by any of the subvalues is possible.  For example:
var airport = {
    "LOXZ": {city: "Zeltweg", altitude_m: 1300 },
    "LOWI": {city: "Innsbruck", altitude_m: 2312 },
    "LOXL": {city: "Linz Hoersching", altitude_m: 1932 },
};
 
#return a list of the hash keys sorted by altitude_m
var sortedkeys= sort (keys(airport), func (a,b) airport[a].altitude_m - airport[b].altitude_m);
 
foreach (var i; sortedkeys)
  print (i, ": ", airport[i].city, ", ", airport[i].altitude_m);
Note that ''sort'' will return errors, and in FG 2.4.0 may even stop working, if the sort function you provide returns errors.  A common cause of this is if your sort vector contains both string and numeric values.  The cmp function will return an error for numeric values, and arithmetic operations you may use to sort numeric values will return errors if performed on a string.  The error in these cases is typically "function/method call on uncallable object".
=== Other useful built-in functions ===
Other basic built-in Nasal functions such as append, setsize, subvec, typeof, contains, delete, int, num, keys, pop, size, streq, cmp, substr, sprintf, find, split, rand, call, die, bind, math.sin, math.pi, math.exp, math.ln math.e, io.read, io.write, regex.exec, and others of that sort,  are detailed in [http://www.plausible.org/nasal/lib.html this external document].
=== Useful functions in the Nasal directory ===
Other functions are available in the Nasal files found in the Nasal directory of a FlightGear install. Simply open those Nasal files in text editor to see what is inside.  Reference those functions by putting the filename in front of the function, method, variable, or object you wish to use.  For instance, to use the method Coord.new() in the file geo.nas, you simply write:
geo.Coord.new()
=== Distance calculations ===
To calculate the distance between two points (in two different ways):
# mylat1, mylong1, mylat2, mylong2 are lat & long in degrees
# myalt1 & myalt2 are altitude in meters
var GeoCoord1 = geo.Coord.new();
GeoCoord1.set_latlon(mylat1, mylong1,myalt1);
var GeoCoord2 = geo.Coord.new();
GeoCoord2.set_latlon(mylat2, mylong2, myalt2);
var directDistance = GeoCoord1.direct_distance_to(GeoCoord2);
var surfaceDistance = GeoCoord1.distance_to(GeoCoord2);
The results are distances in meters.
* distance_to - returns distance in meters along Earth curvature, ignoring altitudes; useful for map distance
* direct_distance_to - returns distance in meters direct; considers altitude, but cuts through Earth surface
=== Other useful geographical functions ===
Other useful geographical functions are found in geo.nas (in the <tt>[[$FG_ROOT]]/Nasal</tt> directory of a FlightGear installation). geo.nas also includes documentation/explanation of the functions available.
== Related content ==
{{Forum|30|Nasal}}
* [[:Category:Nasal]]
=== External links ===
* http://www.plausible.org/nasal


{{Appendix}}
{{Appendix}}


[[Category:Nasal]]
[[Category:Nasal]]

Revision as of 12:13, 19 August 2012

Please note that a considerable amount of resources has not yet been incorporated here, you can check these out by going to the discussion page, where we are collecting links to webpages and mailing list discussions/postings related to Nasal.

FlightGear offers a very powerful functional scripting language called Nasal, which supports reading and writing of internal FlightGear properties, accessing internal data via extension functions, creating GUI dialogs and much more.

WIP.png Work in progress
This article or section will be worked on in the upcoming hours or days.
See history for the latest developments.


References