Howto:Write simple scripts in Nasal: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(begin the tutorial)
 
No edit summary
Line 7: Line 7:
Within that new fokker50/Systems directory then, create an empty plain text file, we'll call it "degftodegc.nas".  Note that many different functions can live in the same .nas file; this could be renamed to something more generic later if other similar functions are added to it in time.
Within that new fokker50/Systems directory then, create an empty plain text file, we'll call it "degftodegc.nas".  Note that many different functions can live in the same .nas file; this could be renamed to something more generic later if other similar functions are added to it in time.


Now, as always add a helpful comment at the top of the file;
It's always nice to start with a helpful comment at the top of the file;


<pre># Converts degF to degC</pre>
<pre># Converts degF to degC</pre>
Now, open our function;
<pre>convertTemp = func{</pre>
What we need to do first is to read in the engine temperature (in degF) from the relevant place in the property tree.  We'll create a local variable for that;
<pre>  var degF = getprop("engines/engine[0]/egt_degf");</pre>
Next, we need to do the actual conversion process.
<pre>  var degC = (degF - 32) * 5/9;</pre>
That seems pretty self-explanatory (but note the trailing semicolon at the end of each line of the function - easy to forget!)
Now that we have a variable in our script containing the temperature in degreesC, we will want to write it into a suitable place in the property tree to allow our instruments to make use of it;
<pre>  setprop("engines/engine[0]/egt-degc", degC);</pre>
This property didn't exist before, but will be created and the value held in our "degC" variable will be written there.  Now that our script seems quite complete we can close the function with a closing curly brace
<pre>}</pre>

Revision as of 10:27, 22 August 2007

Introduction: Nasal is a scripting language available in FlightGear which opens up a world of possibilities for aircraft modellers. Here, as an example, we will create a script to convert engine temperatures from degF to degC, as required by the Fokker50 instruments.

Creating the nasal file: As with many other things in FG, nasal functions can live in many different places. Here, however, we're going to create a "Systems" directory under the Main fokker50 directory. Later, other files might be added to this directory; an electrical system, functions to control other cockpit indicators, etc.

Within that new fokker50/Systems directory then, create an empty plain text file, we'll call it "degftodegc.nas". Note that many different functions can live in the same .nas file; this could be renamed to something more generic later if other similar functions are added to it in time.

It's always nice to start with a helpful comment at the top of the file;

# Converts degF to degC

Now, open our function;

convertTemp = func{

What we need to do first is to read in the engine temperature (in degF) from the relevant place in the property tree. We'll create a local variable for that;

  var degF = getprop("engines/engine[0]/egt_degf");

Next, we need to do the actual conversion process.

  var degC = (degF - 32) * 5/9;

That seems pretty self-explanatory (but note the trailing semicolon at the end of each line of the function - easy to forget!)

Now that we have a variable in our script containing the temperature in degreesC, we will want to write it into a suitable place in the property tree to allow our instruments to make use of it;

  setprop("engines/engine[0]/egt-degc", degC);

This property didn't exist before, but will be created and the value held in our "degC" variable will be written there. Now that our script seems quite complete we can close the function with a closing curly brace

}