Howto:Coding a simple Nasal Framework: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(Created page with "{{WIP}} == Objective == Illustrate the basic thought process required to come up with Nasal/Canvas code that is sufficiently generic to support the following requirements * s...")
 
No edit summary
Line 9: Line 9:


We'll be using the PFD/ND code as an example here, and won't be using any complicated techniques.
We'll be using the PFD/ND code as an example here, and won't be using any complicated techniques.
== Variables ==
In order to support independent instances of each instrument, you need to use separate variables, so rather than having something like this at global scope:
<syntaxhighlight lang="nasal">
var horizon = nil;
var markerBeacon = nil;
var markerBeaconText = nil;
var speedText = nil;
var machText = nil;
var altText = nil;
var selHdgText = nil;
var fdX = nil;
var fdY = nil;
</syntaxhighlight>
You would instead use a hash, and populate it with your variables:
<syntaxhighlight lang="nasal">
var PrimaryFlightDisplay= {
new: func() { return {parents:[PrimaryFlightDisplay],}; },
# set up fields
horizon: nil,
markerBeacon: nil,
markerBeaconText: nil,
speedText: nil,
machText: nil,
altText: nil,
selHdgText: nil,
fdX: nil,
fdY:nil,
};
</syntaxhighlight>
The same thing can be accomplished by doing something like this in your constructor, using a temporary object:
<syntaxhighlight lang="nasal">
var PrimaryFlightDisplay= {
new: func() {
  var m = {parents:[PrimaryFlightDisplay]};
  m.horizon = nil;
  m.markerBeacon = nil;
  m.markerBeaconText = nil;
  m.speedText = nil;
  m.machText = nil;
  m.altText = "Hello World";
  m.selHdgText = nil;
  m.fdX = nil;
  m.fdY = nil;
  return m;
},
};
</syntaxhighlight>
To create a new object, you would then simply have to call the .new() function:
<syntaxhighlight lang="nasal">
var myPFD = PrimaryFlightDisplay.new();
print( myPDF.altText );
</syntaxhighlight>
By using this method, you can easily create dozens of independent instances of your class:
<syntaxhighlight lang="nasal">
var PFDVector = [];
forindex(var i=0;i<100;i+=1)
  append(PFDVector, PrimaryFlightDisplay.new() );
</syntaxhighlight>
== Dealing with Properties ==

Revision as of 17:15, 26 February 2014

WIP.png Work in progress
This article or section will be worked on in the upcoming hours or days.
See history for the latest developments.

Objective

Illustrate the basic thought process required to come up with Nasal/Canvas code that is sufficiently generic to support the following requirements

  • support multiple independent instances (e.g. PFDs or NDs)
  • be aircraft/use-case agnostic (e.g. work without hard-coded properties)
  • configurable without touching back-end code (e.g. via configuration hashes)
  • modular (use separate files for splitting things up)

We'll be using the PFD/ND code as an example here, and won't be using any complicated techniques.


Variables

In order to support independent instances of each instrument, you need to use separate variables, so rather than having something like this at global scope:

var horizon = nil;
var markerBeacon = nil;
var markerBeaconText = nil;
var speedText = nil;
var machText = nil;
var altText = nil;
var selHdgText = nil;
var fdX = nil;
var fdY = nil;

You would instead use a hash, and populate it with your variables:

var PrimaryFlightDisplay= {
 new: func() { return {parents:[PrimaryFlightDisplay],}; },
 # set up fields
 horizon: nil,
 markerBeacon: nil,
 markerBeaconText: nil,
 speedText: nil,
 machText: nil,
 altText: nil,
 selHdgText: nil,
 fdX: nil,
 fdY:nil,
};

The same thing can be accomplished by doing something like this in your constructor, using a temporary object:

var PrimaryFlightDisplay= {
 new: func() {
  var m = {parents:[PrimaryFlightDisplay]}; 
  m.horizon = nil;
  m.markerBeacon = nil;
  m.markerBeaconText = nil;
  m.speedText = nil;
  m.machText = nil;
  m.altText = "Hello World"; 
  m.selHdgText = nil;
  m.fdX = nil;
  m.fdY = nil;
 
  return m;
 },
};

To create a new object, you would then simply have to call the .new() function:

 var myPFD = PrimaryFlightDisplay.new();
 print( myPDF.altText );

By using this method, you can easily create dozens of independent instances of your class:

 var PFDVector = [];
 forindex(var i=0;i<100;i+=1)
  append(PFDVector, PrimaryFlightDisplay.new() );

Dealing with Properties