Howto:Create a new system in Nasal: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
m (Category sorting)
mNo edit summary
Line 24: Line 24:
* paste the following code into this file using a text editor (such as notepad on Windows)
* paste the following code into this file using a text editor (such as notepad on Windows)


<syntaxhighlight lang="php">
  # test.nas (save in $FG_ROOT/Nasal)
  # test.nas (save in $FG_ROOT/Nasal)
  var mycode = func {
  var mycode = func {
Line 31: Line 32:
   
   
  _setlistener("/sim/signals/nasal-dir-initialized", mycode);
  _setlistener("/sim/signals/nasal-dir-initialized", mycode);
 
</syntaxhighlight>


This piece of code defines a new function named "mycode" that just prints a message to the console.
This piece of code defines a new function named "mycode" that just prints a message to the console.

Revision as of 21:16, 5 January 2012

This article is a stub. You can help the wiki by expanding it.

Status: Planning

Last updated: 12/2010

Authors: Hooray

References:


This tutorial will document all the steps involved in creating a new Nasal module that constantly updates itself, Nasal is FlightGear's scripting language.

This tutorial is based on Howto: Create a new Nasal module, you should fully understand that one first.

A system that constantly updates itself at a fixed rate could for example be used to simulate a new instrument in FlightGear.

Please keep in mind that Nasal functions that are invoked this way are obviously limited by the framerate of the simulator (i.e. if your framerate is 50 hps, you cannot run a Nasal function at higher rates). Update rates of a couple hertz are fine however. Keep in mind though, that the time spent in each Nasal function will obviously also have an effect (on framerate, too).


  • create a new plain text file under $FG_ROOT/Nasal/MODULE.nas
  • replace MODULE with the name for your new module (for example, use "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("running mycode() ");
   settimer(mycode, 3); # register a timer, invoke the "mycode" function again after 3 seconds
 }
 
 _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. Next, a timer is registered to invoke the mycode function again after 3 seconds have passed.

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