Creating new Nasal scripts
| The FlightGear forum has a subforum related to: Nasal Scripting |
| Nasal scripting |
|---|
| Nasal internals |
|---|
Nasal scripts can be placed in multiple locations.
Creating new Scripts
Nasal scripts need to be plain text files, saved with a *.nas extension. They can be created and written using a conventional plain text editor like notepad, gedit, kate etc. But you can also use a programming text editor with syntax lighting support, see Howto:Syntax highlighting for Nasal.
Aircraft specific Nasal code
Generally, aircraft specific Nasal scripts reside in the corresponding aircraft's folder (or a corresponding /Nasal subfolder) where they are usually included by adding a corresponding <nasal> tag to the aircraft-set.xml file (see Writing simple scripts in "nasal"). Also see the section on namespaces which contains more specific examples.
Instrument specific Nasal code
While instrument specific scripts are saved within the instrument's folder (as previously mentioned, Nasal scripts can also be embedded in various other XML files), Nasal scripts driving shared instruments are generally stored in $FG ROOT/Aircraft/Generic/.
Nasal code as bindings in XML files
Nasal scripts can also be used as "binding" objects, and can therefore appear anywhere in a configuration file (keyboard, mouse and joystick bindings, etc...) that accepts a <binding> tag. The relevant command type is "nasal", and you place your Nasal code inside of the <script> tag:
<binding>
<command>nasal</command>
<script>
print("Binding Invoked!");
</script>
</binding>
The code above invokes the print() function. This is a simple extension function that simply prints out its arguments, in order, to the FlightGear console as a single-line log entry. It is useful for debugging, but little else.
System-wide Nasal code
Nasal scripts that are not specific to certain aircraft, instruments or other uses, generally reside in the system-wide $FG ROOT/Nasal directory. Scripts that are placed inside this directory (with a *.nas extension) are automatically loaded and run during FlightGear startup.
Nasal sub modules
You can use io.include(file); to import other Nasal modules.
User specific Nasal scripts
It's also possible to put Nasal files into $FG_HOME/Nasal/, that is: ~/.fgfs/Nasal/ on Unix, and %APPDATA%\flightgear.org\Nasal\ on MS Windows. This has the following advantages:
- one doesn't have to mix local extensions with standard files
- one is less likely to lose such local additions when upgrading
- one doesn't need write permission to $FG_ROOT/Nasal/ or
- one doesn't have to become "root" to edit such files
The files are guaranteed to be read after all the files in $FG_ROOT/Nasal/, so one can safely use elements of files like props.nas (props.Node), or globals.nas (setlistener() without leading underscore).
The files are guaranteed to be read in alphabetic order. So, if there are two files where one depends on the other, just name them appropriately.
The contents of each file are added to a namespace derived from the filename. So, all functions and variables of a file ~/.fgfs/Nasal/local.nas will be added to nasal namespace "local", and a function test() is globally accessible as local.test().
It's possible to extend a standard module like "math" with definitions in ~/.fgfs/Nasal/math.nas, though this should, of course, not be exploited by code that is to be submitted to cvs.