Howto:Coding a simple Nasal Framework: Difference between revisions

Jump to navigation Jump to search
m
m (→‎Modularization: layout: Moving image to top of text in messagebox)
Line 10: Line 10:
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.


== Classes as Containers for your Variables ==
{{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/>
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/>
If you'd like to learn more about using classes and objects (instance variables) to accomplish this, see this little tutorial: [[Howto:Coding_a_simple_Nasal_Framework]]<br/>
<br/>
Basically, the idea is to get rid of "global" variables, and instead use "instance" variables that are part of an outer scope (hash), such as:
  |{{cite web |url=http://forum.flightgear.org/viewtopic.php?p=213105#p213105
    |title=<nowiki>Re: 777 EFB: initial feedback</nowiki>
    |author=<nowiki>Hooray</nowiki>
    |date=<nowiki>Sat Jun 21</nowiki>
  }}
}}
<syntaxhighlight lang="nasal">
var EFB = {
# constructor (for making new EFB objects)
new: func(name) {
# create a new EFB object, inherited from EFB class
var m = {parents:[EFB] };
# add a new field to the class named "name", assign a value to it
m.name = name;
return m; # return the whole thing to the caller
},
# define a method (class function) that can be called to print out the name of the EFB
whoami: func() {
  print("EFB owner:", me.name );
}
};
var CaptainEFB = EFB.new("captain");
var CopilotEFB = EFB.new("copilot");
CaptainEFB.whoami();
CopilotEFB.whoami();
</syntaxhighlight>
{{FGCquote
  |(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/>
  |{{cite web |url=http://forum.flightgear.org/viewtopic.php?p=213105#p213105
    |title=<nowiki>Re: 777 EFB: initial feedback</nowiki>
    |author=<nowiki>Hooray</nowiki>
    |date=<nowiki>Sat Jun 21</nowiki>
  }}
}}


== Variables ==
== Variables ==

Navigation menu