Howto:Start using vectors and hashes in Nasal

From FlightGear wiki
Revision as of 19:01, 7 February 2012 by Hooray (talk | contribs) (in response to some Nasal code that I've seen recently)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
This article is a stub. You can help the wiki by expanding it.

Objective: Learn more about vectors and hashes to see how you can simplify and generalize your code easily.

Imagine a piece of Nasal code working with 5 different waypoints. You would need to manage the data for each waypoint. A simple version might look like this:

      var wp1 = 0;
      var wp1alt = 0;
      var wp1dist = 0;
      var wp1angle = 0;
      var wp1length = 0;
      var wp2 = 0;
      var wp2alt = 0;
      var wp2dist = 0;
      var wp2angle = 0;
      var wp2length = 0;
      var wp3 = 0;
      var wp3alt = 0;
      var wp3dist = 0;
      var wp3angle = 0;
      var wp3length = 0;
      var wp4 = 0;
      var wp4alt = 0;
      var wp4dist = 0;
      var wp4angle = 0;
      var wp4length = 0;
      var wp5 = 0;
      var wp5alt = 0;
      var wp5dist = 0;
      var wp5angle = 0;
      var wp5length = 0;
      var wp1id = "";
      var wp2id = "";
      var wp3id = "";
      var wp4id = "";
      var wp5id = "";
      var wp1brg = 0;
      var wp2brg = 0;
      var wp3brg = 0;
      var wp4brg = 0;
      var wp5brg = 0;

Now, this is fairly repetitive and not overly scalable, because the variable names are hard coded and need to be changed in a lot of places in the source code. So, it would be better to use a vector of waypoints instead:


var waypoints = ["wp1","wp2","wp3","wp4","wp5"];

As you can see below, indexing starts at 0. The problem is, that this only gives us a list of waypoints:

var waypoints = [1,2,3,4,5];
 print( waypoints[0] ); # prints wp1
 print( waypoints[1] ); # prints wp2
 print( waypoints[2] ); # wp3
 print( waypoints[3] ); # wp4
 print( waypoints[4] ); # wp5

What we really need to save all the other waypoint specific information is a new variable type that serves as the "container" for variables:

var wp4 = 0; # waypoint number
var wp4alt = 0; # waypoint altitude
var wp4dist = 0; # waypoint distance
var wp4angle = 0; #waypoint angle
var wp4length = 0; # waypoint length
var wp4id = "";       # waypoint id
var wp4brg = 0; # waypoint bearing

Now, to wrap these fields into a single variable, we could use a Nasal hash. Consider the following empty hash:

var waypoint = {};

Next, we are going to add some fields to our new variable "container":

var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0};

This adds the following "fields" to the waypoint hash and sets their initial value to 0:

  • number
  • altitude
  • distance
  • angle
  • length
  • ID
  • bearing

Now, to access any of these fields, we would use the "dot notation" by first specifying the name of the enclosing context (which is really just a fancy word for the name of the hash) and the name of the field we are interested in:

var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0};
print ( waypoint.number );
print ( waypoint.altitude );
print ( waypoint.distance );
print ( waypoint.angle );
print ( waypoint.length );
print ( waypoint.ID );
print ( waypoint.bearing );

Now, to make this is a little more interesting and to show what's happening, we are going to change the value of each field:

var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
print ( waypoint.number ); # prints 1
print ( waypoint.altitude ); # prints 2
print ( waypoint.distance ); # prints 3
print ( waypoint.angle ); # prints 4
print ( waypoint.length ); # 5
print ( waypoint.ID ); #6
print ( waypoint.bearing ); #7

So, you could obviously create several different versions of this hash to store your waypoint data:

var waypoint1 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint2 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint3 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint4 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint5 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};

This is already a pretty cool thing, because you would end up with 5 different containers all having their own set of fields, that you can access and set arbitrarily - without affecting the other containers.

But the really cool thing comes next:

As you may have noticed, the code for each waypoint is 100% identical - so we could just as well tell the Nasal engine to use an existing hash as a TEMPLATE for a new object. This is accomplished using the "parents" keyword:

var Position3D = {x:1.00, y:2.00, z:3.00};
var p1 = { parents: [Position3D] };
var p2 = { parents: [Position3D] };
var p3 = { parents: [Position3D] };

This creates three different new containers/hashes by copying the fields from the hash specified in the parents vector.

What parents will do is this: whenever a hash contains a vector field named "parents:" pointing to other hashes, it will look up the parent hashes and use them as a template and copy the fields of the parent hashes to the new hash, i.e. less typing for you!

This means, that the previously posted code could be easily abbreviated and written like this:

var waypoint1 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint2 = {parents:[waypoint1] };
var waypoint3 = {parents:[waypoint1] };
var waypoint4 = {parents:[waypoint1] };
var waypoint5 = {parents:[waypoint1] };

Now, given that this is such a common thing to do - we could just as well add a new function to the parent hash that we can use to construct new hashes. As you could see already, the fields (or members) of a hash are specified in a well-defined form: "field_name:value".

var waypoint1 = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7
};

This isn't any different for fields that are of type "function":


var waypoint = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7,
hello: func {
 print("Hello");
}
};

Note that we have added a new field named "hello" to our waypoint hash. This can be easily accessed and also called:

var waypoint = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7,
hello: func {
 print("Hello");
}
};

var w = {parents: [waypoint]};
w.hello();

On the other hand, we were just about to make construction of such hashes much simpler. So we are going to change the function and make it return a new hash that is properly set up:

var waypoint = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7,
hello: func {
 return {parents:[waypoint]}
}
};

Note how the hello function has now been modified to return a new hash to its caller. So we could just change its name to something more telling like "new":

var waypoint = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7,
new: func {
 return {parents:[waypoint]}
}
};


So, whenever we need a new hash object of type "waypoint", we can simply call this construction function (which we'll call a constructor from now on):

var wp = waypoint.new();

Now, let's imagine you want to add another member function to print a certain field's value, such as "number". This can be accomplished using the "me" keyword which ensures that the member function is always referring to the active object:


var waypoint = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7,
new: func {
 return {parents:[waypoint]};
},
show_number: func {
 print(me.number);
}
};

On the other hand, maybe you'd like to add some information (such as the number) during construction time to the object. So this would require changing the constructor function to accept a parameter, too:

var waypoint = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7,
new: func(n) {
 return {parents:[waypoint]};
},
show_number: func {
 print(me.number);
}
};

Note how the new function has been changed to accept a parameter named "n".

Now, to actually set the number field during construction time to the value of n, you could create a temporary hash:

new: func(n) {
 var t={parents:[waypoint]};
 t.number = n;
 return t;
}

Or in its entirety:

var waypoint = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7,
new: func(n) {
 var t={parents:[waypoint]};
 t.number = n;
 return t;
},
show_number: func {
 print(me.number);
}
};

Now, back to your initial example regarding a list of waypoints:


var waypoint = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7,
new: func(n) {
 var t={parents:[waypoint]};
 t.number = n;
 return t;
},
};

var waypoints = [waypoint.new(), waypoint.new(), waypoint.new(), waypoint.new(), waypoint.new() ];

Note how this create a vector of 5 waypoints (0..4), each of these waypoints is a full object that can be conveniently accessed:

waypoints[0].bearing = 100;
waypoints[1].altitude = 100;
waypoints[2].ID = "none";

You can also use Nasal's support for looping to conveniently process these waypoints:

var waypoint = { 
number:1,
altitude:2,
distance:3,
angle:4,
length:5,
ID:6,
bearing:7,
new: func(n) {
 var t={parents:[waypoint]};
 t.number = n;
 return t;
},
dump: func {
 print("Altitude:", me.altitude, " distance:", me.distance, " Bearing:", me.bearing);
}
};

var waypoints = [waypoint.new(), waypoint.new(), waypoint.new(), waypoint.new(), waypoint.new(), ];

foreach(var w; waypoints) {
 w.dump();
}


This was a very simple introduction to object oriented programming in Nasal.