Howto:Coding a simple Nasal Framework: Difference between revisions

No edit summary
Line 81: Line 81:


== Dealing with Properties ==
== Dealing with Properties ==
The next complication is dealing with instrument-specific properties. Typically, code will have lots of getprop()/setprop() equivalent calls in many places. These need to be encapsulated, i.e. you don't want to use setprop/getprop (or prop.nas) directly for any properties that are specific to the instrument, otherwise your code would fail once several instances of it are active at the same time, because their property access may be competing/conflicting, such as overwriting properties.
One simple solution for this is to have your own setprop/getprop equivalent, as part of your class:
<syntaxhighlight lang="nasal">
var PrimaryFlightDisplay = {
set: func(property, value) {
},
get: func(property, default) {
},
};
</syntaxhighlight>
Your methods would then never use  setprop/getprop directly, but instead use the set/get methods, by calling '''me.get()''' or '''me.set()''' respectively.
The next step is identifying property that are instance-specific, i.e. that must not be shared with other instruments. One convenient way to accomplish this is using a numbered index for each instance, such as: '''/instrumentation/pfd[0]''', '''/instrumentation/pfd[1]''', '''/instrumentation/pfd[2]''' etc.
This would then be the place for your instance-specific properties.