Nasal scripting language: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(Adding multiple listeners to a function)
mNo edit summary
Line 15: Line 15:


A listener is attaches to a property. Listeners do not work with "tied" properties (most are in the FDM, but you can spot them by Ctrl-clicking "." in the property browser).
A listener is attaches to a property. Listeners do not work with "tied" properties (most are in the FDM, but you can spot them by Ctrl-clicking "." in the property browser).
Listeners are useful when you need to monitor a value. They are most efficient when the value does not change frequently, but are generally more efficient and better than a timer.


Setting a listener:
Setting a listener:

Revision as of 01:32, 24 June 2006

Many applications today support scripting as a means of "gluing" the application together and enabling users to extend functionality of the application. For example, the 3d modeling application Blender implements most of its import and export filters as user contributed Python scripts, which are automatically integrated into the menu system. Many games are also integrating a scripting language to make it easier for modders to add features.

FlightGear has a scripting language called NASAL. You can find out more about it at the NASAL website.

http://www.plausible.org/nasal

Elements of the graphical user interface, aircraft instruments and performance can be scripted.

NASAL Scripting for Flight Gear

Listeners

A listener specifies a function to be executed when a specified property is written to. The value may or may not change, so the listener may execute "on change" if the property value changes, but also if it is rewritten with the same value. The listener executes whenever the property is written to.

A listener is attaches to a property. Listeners do not work with "tied" properties (most are in the FDM, but you can spot them by Ctrl-clicking "." in the property browser).

Listeners are useful when you need to monitor a value. They are most efficient when the value does not change frequently, but are generally more efficient and better than a timer.

Setting a listener:

setlistener( [Property], [Function] );

Note: The name of the function is never quoted in a listener.

monitor_course = func {
  print("Monitoring course.");
}
setlistener("instrumentation/gps/wp/leg-course-deviation-deg", monitor_course);

Deleting a listener:

removelistener( [Function] );


removelistener(monitor_course);


Setting more than one listener on a given function:

kernel = func {
     flaps = getprop(Controls, "flight/flaps");
     if( flaps == 20 ) {
         print("Flaps 20");
     }
     if( flaps == 40 ) {
         print("Flaps 40");
     }
     if( flaps == 60 ) {
         print("Flaps 60");
     }
}
setlistener("/controls/flight/flaps", kernel);
setlistener("/controls/flight/flaps-serviceable", kernel);


Returning from a listener early:

monitor_course = func {
  if( getprop(Internal, "power-good") == "off" ) {
     return 0;
  }
  print("Monitoring course."); 
}

Examples:

TODO


Tips

How to play sounds using NASAL script?

The same way one would do animations using Nasal;

Adjust properties in Nasal (they may be "private" properties in your own subtree of the property list, say /tmp/<aircraft>) and let the sound configuration file act on those properties. (from flightgear-devel)