Howto:Extend Nasal: Difference between revisions

Jump to navigation Jump to search
m
some more examples, untested/uncompiled at the moment
m (even more Nasal docs)
 
m (some more examples, untested/uncompiled at the moment)
Line 6: Line 6:
The simplest way to do this is to look at the existing functions in [http://cvs.flightgear.org/viewvc/source/src/Scripting/ $FG_SRC/Scripting]/NasalSys.cxx (src/Scripting/NasalSys.cxx).
The simplest way to do this is to look at the existing functions in [http://cvs.flightgear.org/viewvc/source/src/Scripting/ $FG_SRC/Scripting]/NasalSys.cxx (src/Scripting/NasalSys.cxx).


There is a static table of function pointers referencing extension functions:
There is a static table of function pointers referencing extension functions, along with their corresponding names in Nasal:


   // Table of extension functions.  Terminate with zeros.
   // Table of extension functions.  Terminate with zeros.
Line 31: Line 31:
     { 0, 0 }
     { 0, 0 }
   };
   };
You will need to add your new extension function to this list of static functions, preferably following the existing naming convention (i.e. "f_" prefix).


= Extension Function Signature =
= Extension Function Signature =
Line 40: Line 42:
   }
   }


If you don't have anything else to return, you can just return naNil().
So, this is basically the boilerplate that you'll need to use in order to expose a new function.
 
If you don't have anything else to return, you can just return naNil(), so that the function looks like this:
 
  static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
  {
      return naNil();
  }
 
In order to make this function known to the Nasal interpreter, you'll want to extend the previously mentioned list of Nasal extension functions, to read:
 
  // ....
    { "airportinfo", f_airportinfo },
    { "cool", f_cool }, //this is where we add our new function
    { 0, 0 }
  };
 
Once you have made these modifications, you can simply invoke your new function, of course it will not yet do anything useful, though.
 
So, in order to make it slightly more interesting, we are going to change the return statement to return something else, instead of nil:
 
  static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
  {
      const char* cool="cool";
      naRef retval;
      naStr_fromdata(retval, cool, strlen(cool) );
      return retval;
  }
 
So, after rebuilding the FlightGear binary, whenever you call the new "cool" API function, it will always return a "cool" string.


= Argument Processing =  
= Argument Processing =  
2,561

edits

Navigation menu