Howto:Basic OOP Programming: Difference between revisions
Legoboyvdlp (talk | contribs) No edit summary |
Legoboyvdlp (talk | contribs) No edit summary |
||
Line 7: | Line 7: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Then, we need to create the class: | Then, we need to create the class for the screen: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var screen = { | var screen = { | ||
Line 69: | Line 69: | ||
setlistener("/sim/signals/fdm-initialized", func { # listens to this property. When it changes, we then run the init loop and start the timer. | setlistener("/sim/signals/fdm-initialized", func { # listens to this property. When it changes, we then run the init loop and start the timer. | ||
systems.ELEC.init(); | systems.ELEC.init(); | ||
timer.start(); | timer.start(); | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 13:52, 10 December 2017
First, we need to define some variables:
# Define variables here to prevent them being recreated at every run of the loop() function
var power_consumption = nil;
var screen_power_consumption = nil;
var screens = nil;
Then, we need to create the class for the screen:
var screen = {
# first, create the attributes of the screen
name: "",
type: "",
max_watts: 0,
dim_watts: 0,
dim_prop: "",
power_consumption: func() { # next we have a function that measures the power consumption and returns it
var dim_prop = me.dim_prop;
if (getprop(me.dim_prop) != 0) { # if the screen is not off
screen_power_consumption = (50 + (10 * getprop(dim_prop))); # y = 50 + 10d, where d = the value of the dimming property.
} else { # if the screen is off, it consumes no power.
screen_power_consumption = 0;
}
return screen_power_consumption; # this means that when you run this function, you will get a value returned to you.
},
new: func(name,type,max_watts,dim_watts,dim_prop) { # finally the creator function, that creates instances of the class
var s = {parents:[screen]};
s.name = name;
s.type = type;
s.max_watts = max_watts;
s.dim_watts = dim_watts;
s.dim_prop = dim_prop;
return s;
}
};
Next, we create a new class called ELEC.
In the first part, we have a function called whenever the FDM initialization is complete: inside it, various instances of that class are created using the new() function. Then, in the second part, we have a loop function that actually does the hard work of calculating the power consumption per screen.
var ELEC = {
init: func() { # initialization function
# Create the instances of the screen class, and add them to a vector in order to use a foreach loop
screens = [screen.new("DU1","LCD",60,50,"controls/lighting/DU/du1"),
screen.new("DU2","LCD",60,50,"controls/lighting/DU/du2"),
screen.new("DU3","LCD",60,50,"controls/lighting/DU/du3"),
screen.new("DU4","LCD",60,50,"controls/lighting/DU/du4"),
screen.new("DU5","LCD",60,50,"controls/lighting/DU/du5"),
screen.new("DU6","LCD",60,50,"controls/lighting/DU/du6")];
},
loop: func() {
foreach(var screena; screens) { # note that we use a different variable, screena
power_consumption = screena.power_consumption(); # call the power_consumption() for each instance of the screen class
setprop("/systems/electrical/DU/" ~ screena.name ~ "/watts",power_consumption); # write the result to a property
}
},
};
Finally, we add the updating function: the following code creates a maketimer and a setlistener that execute the loop(); and init(); functions respectively:
var systemsLoop = maketimer(0.1, func {
systems.ELEC.loop(); # runs the loop(); function every 0.1 seconds
});
setlistener("/sim/signals/fdm-initialized", func { # listens to this property. When it changes, we then run the init loop and start the timer.
systems.ELEC.init();
timer.start();
});
These are the basic parts of OOP. This code should be reasonable standalone, in that if you setprop some values (eg the dimming properties) it should work.