Howto:Coding a simple Nasal Framework: Difference between revisions

Jump to navigation Jump to search
m
Line 13: Line 13:
== Classes as Containers for your Variables ==
== Classes as Containers for your Variables ==
{{FGCquote
{{FGCquote
   |reading up a little more on OO may help you generalize some of your code a little better - for example, it seems that your code is currently structured such that it only supports a single instance of your EFB ? Once you start using classes and objects, you can easily re-arrange your code to allow your captain/copilot to have independent instances of your EFB, so that they don't affect each other. In fact, you could theoretically have dozens of EFBs running concurrently. This may not seem useful or relevant to you at the moment, but it greatly simplifies coding in the long-term.<br/>
   |reading up a little more on OO (object-oriented programming) may help you generalize some of your code a little better - for example, it seems that your code is currently structured such that it only supports a single instance of your EFB ? Once you start using classes and objects, you can easily re-arrange your code to allow your captain/copilot to have independent instances of your EFB, so that they don't affect each other. In fact, you could theoretically have dozens of EFBs running concurrently. This may not seem useful or relevant to you at the moment, but it greatly simplifies coding in the long-term.<br/>
Gijs ND/PFD code had the same problem originally - but you will find that it is much easier to write generic code once you start using separate instances/variables for each "version" of your instrument (EFB).<br/>
Gijs ND/PFD code had the same problem originally - but you will find that it is much easier to write generic code once you start using separate instances/variables for each "version" of your instrument (EFB).<br/>
<br/>
<br/>
Line 27: Line 27:
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var EFB = {
var EFB = {
  # constructor (for making new EFB objects)
  # constructor (for making new EFB objects)
  new: func(name) {
  new: func(name) {
  # create a new EFB object, inherited from EFB class
  # create a new EFB object, inherited from EFB class
  var m = {parents:[EFB] };
  var m = {parents:[EFB] };
  # add a new field to the class named "name", assign a value to it
  # add a new field to the class named "name", assign a value to it
  m.name = name;
  m.name = name;
  return m; # return the whole thing to the caller
  # here you can add other fields that shall be specific to the instance/object, e.g. the root property
# and finally return the new object to the caller
return m;
  },
  },
  # define a method (class function) that can be called to print out the name of the EFB
  # define a method (class function) that can be called to print out the name of the EFB
Line 50: Line 57:
{{FGCquote
{{FGCquote
   |(You can use the Nasal console to test this)<br/>
   |(You can use the Nasal console to test this)<br/>
As you can see, your two EFBs will inherit from the same EFB class, but they will have their own private namespace - i.e. the "name" member in this case. It can be accessed via the "me" prefix.<br/>
As you can see, your two EFBs will inherit from the same EFB class, but they will have their own private namespace - i.e. the "name" member in this case. It can be accessed via the "me" prefix. And each instance (object) will have its own scope, referenced via the '''me''' keyword.<br/>


   |{{cite web |url=http://forum.flightgear.org/viewtopic.php?p=213105#p213105
   |{{cite web |url=http://forum.flightgear.org/viewtopic.php?p=213105#p213105

Navigation menu