Nasal scripting language

From FlightGear wiki
Jump to navigation Jump to search
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.


Hello world

A simple hello world example in Nasal would be:

 # hello.nas
 print('Hello World!');

This will show the "Hello World" string during startup in the console window. The hash sign (#) just introduces comments (i.e. will be ignored by the interpreter).

Note: Script-specific symbols such as global variables (or functions) will be put into a scope (namespace) based on the script's name, scripts embedded via aircraft-set.xml files can separately specify a corresponding module name (see Howto: Make an aircraft for details).

Strings in Nasal can also use double quotes which support escaping:

 # hello.nas
 print("Hello\nWorld!");

Double quotes support typical escape sequences:

  • \n Newline
  • \t Horizontal Tab
  • \v Vertical Tab
  • \b Backspace
  • \r Carriage Return
  • \f Form feed
  • \a Audible Alert (bell)
  • \\ Backslash
  • \? Question mark
  • \' Single quote
  • \" Double quote

For example, to print a new line, use:

print ("\n");

To print a quoted string, use:

print ("\"quoted string\"");

and so on.

Single quotes treat everything as literal except for embedded single quotes (including embedded whitespace like newlines).

Nasal strings are always arrays of bytes (never characters: see the utf8 library if you want character-based equivalents of substr() et. al.). They can be indexed just like in C (although note that there is no nul termination -- get the length with size()):

Editing code files

Note that there is currently no way to tell FlightGear to reload Nasal scripts from the global Nasal directory at runtime, so in order to see changes take effect, you will have to exit and restart FlightGear for the time being. Note that there are some workarounds available, see: reloading Nasal code without re-starting FlightGear.

Also, note that as of 05/2009, Nasal in FlightGear does not yet support any form of dependency resolution. In other words, there's no "import", "require" or "include" directive - this is also why most code in FlightGear is wrapped inside a _setlistener() call instead, which in turn waits for a FlightGear signal before executing the code (see below for details).

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 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 $FG_ROOT/Nasal directory of a FlightGear installation). geo.nas also includes documentation/explanation of the functions available.

Related content

External links

References