Howto:Create a new Nasal module

From FlightGear wiki
Jump to navigation Jump to search
Note  This article needs to be reviewed/updated: because fgdata must be considered read-only.

This tutorial will document all the steps involved in creating a new Nasal module. Nasal is FlightGear's built-in scripting language.

  • Create a new plain text file under $FG_ROOT/Nasal/<module_name>.nas
  • Replace <module_name> with the name for your new module (for example test)
  • Paste the following code into this file using a text editor (such as Notepad on Windows)
# test.nas (save in $FG_ROOT/Nasal)
var mycode = func() {
    print("My test module got loaded!");
};
setlistener("/sim/signals/nasal-dir-initialized", mycode);

This piece of code defines a new function named mycode that just prints a message to the console (terminal/shell window).

Note  This code does not print or output anything to the window of the FlightGear simulator screen, only the console window.

The setlistener() call at the end of the snippet waits for the Nasal system to be initialized before the mycode function gets called.

To change the code such that it also prints something to the FlightGear window, you can use gui.popupTip() to show a tooltip for a configurable duration.

# test.nas (save in $FG_ROOT/Nasal)
var mycode = func() {
    var message = "My test module got loaded!";
    print(message); # prints the message to the black console/terminal window
    gui.popupTip(message, 5); # shows the message inside the main FlightGear window as a popup/tooltip for a duration of 5 seconds
}; # end of the mycode function

# sets up a signal handler that listens to the specified property
# once it changes, it triggers the mycode callback function specified above 
setlistener("/sim/signals/nasal-dir-initialized", mycode);