Howto:Create a new system in Nasal
This article is a stub. You can help the wiki by expanding it. |
The FlightGear forum has a subforum related to: Nasal Scripting |
Nasal scripting |
---|
Nasal internals |
---|
Memory Management (GC) |
This tutorial will document all the steps involved in creating a new Nasal module that constantly updates itself.
This tutorial is based on Howto: Create a new Nasal module, so 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 frame rate of the simulator (e.g., if your frame rate is 50 fps, 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 the frame rate, too).
Steps:
- Create a new plain text file under $FG_ROOT/Nasal/<module>.nas, replacing <module> with the name of 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()");
}
var timer = maketimer(3, mycode); # register a timer, invoke the "mycode" function again after 3 seconds
# wait for the Nasal subsystem to be up and running before starting the timer
setlistener("/sim/signals/nasal-dir-initialized", func { timer.start(); });
Caution If you want to pass arguments to your myCode callback, you need to prepend the func keyword, or the callback will be invoked immediately |
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 setlistener()
call at the end of the snippet waits for the Nasal system to be initialized before the "mycode" function gets called.