Howto:Write simple scripts in Nasal: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 32: Line 32:


<pre>}</pre>
<pre>}</pre>
'''Including the nasal file in the -set file:'''
To make sure our aircraft "knows" about this nasal file, we will need to add a section to the main aircraft -set.xml (in this case, fokker50-set.xml of course).
Here's the relevant XML, which I put at the bottom immediately above the closing </propertylist>:
<pre>
<nasal>
  <fokker50>
  <file>Aircraft/fokker50/Systems/degftodegc.nas</file>
  </fokker50>
</nasal>
</pre>
'''Crunch Time, or, testing our function:'''
Now, we can start FlightGear to test our new function. Start FG in the usual manner with the appropriate aircraft.

Revision as of 10:32, 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

}

Including the nasal file in the -set file: To make sure our aircraft "knows" about this nasal file, we will need to add a section to the main aircraft -set.xml (in this case, fokker50-set.xml of course).

Here's the relevant XML, which I put at the bottom immediately above the closing </propertylist>:

 <nasal>
  <fokker50>
   <file>Aircraft/fokker50/Systems/degftodegc.nas</file>
  </fokker50>
 </nasal>


Crunch Time, or, testing our function: Now, we can start FlightGear to test our new function. Start FG in the usual manner with the appropriate aircraft.