Howto:Write simple scripts in Nasal: Difference between revisions

Jump to navigation Jump to search
no edit summary
mNo edit summary
No edit summary
Line 52: Line 52:
Now, call up the "nasal console" from the debug menu (only available in recent versions of FG.)  In a blank tab, type
Now, call up the "nasal console" from the debug menu (only available in recent versions of FG.)  In a blank tab, type


<pre>fokker50.convertTemp();</pre> and press execute.
<pre>fokker50.convertTemp();</pre> and press execute. (The "fokker50" part comes from the entry we made in the -set xml file)


The "fokker50" part comes from the entry we made in the -set xml file.
[[Image:nasal_console.jpg]]
[[Image:nasal_console.jpg]]
You can see in the property browser (file/browse internal properties) that we now have a new egt property for the first engine (/engines/engine), with the value in degC - success!
However, you will also see that it doesn't change, unless you execute the function again.  We need to add some more to our script in order to have it run continuously; add this to the end of your function (before the closing curly brace though!)
<pre>  settimer(convertTemp, 0.1);</pre>
Settimer will call the convertTemp function, and the second argument determines the frequency with which it does so.  If your function requires as rapid a response as possible, use 0.
Now, our function, once called, will constantly update the degC property - but we still have to call it manually.  In order to start the function, we'll use a "listener"; this "watches" a given property and calls the desired function whenever that property is written to.  This is an ideal, efficient method to use for things like switches which don't change state very often.
Place this AFTER the close of the convertTemp function:
<pre>setlistener("sim/signals/fdm-initialized", convertTemp);</pre>
This listens for the fdm-initialized property to be set (this will happen when the sim has started up), and starts our convertTemp function when that happens.
57

edits

Navigation menu