Howto:Basic OOP Programming: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(Basic OOP tutorial)
 
No edit summary
Line 1: Line 1:
First, we need to define some variables:
First, we need to define some variables:
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
# Define variables here to prevent them being recreated at every run of the loop() function
var power_consumption = nil;
var power_consumption = nil;
var screen_power_consumption = nil;
var screen_power_consumption = nil;
Line 17: Line 18:
power_consumption: func() { # next we have a function that measures the power consumption and returns it
power_consumption: func() { # next we have a function that measures the power consumption and returns it
var dim_prop = me.dim_prop;
var dim_prop = me.dim_prop;
if (getprop(me.dim_prop) != 0) {
if (getprop(me.dim_prop) != 0) { # if the screen is not off
screen_power_consumption = (50 + (10 * getprop(dim_prop)));
screen_power_consumption = (50 + (10 * getprop(dim_prop))); # y = 50 + 10d, where d = the value of the dimming property.
} else {
} else { # if the screen is off, it consumes no power.
screen_power_consumption = 0;
screen_power_consumption = 0;
}  
}  
return screen_power_consumption;
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
new: func(name,type,max_watts,dim_watts,dim_prop) { # finally the creator function, that creates instances of the class
Line 38: Line 39:
</syntaxhighlight>
</syntaxhighlight>


Next, we create various instances of that class using the new() function: the following code is executed once the FDM initialization is complete:
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.
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
# Create the instances of the screen class, and add them to a vector in order to use a foreach loop
var ELEC = {
screens = [screen.new("DU1","LCD",60,50,"controls/lighting/DU/du1"),
init: func() { # initialization function
screen.new("DU2","LCD",60,50,"controls/lighting/DU/du2"),
              # Create the instances of the screen class, and add them to a vector in order to use a foreach loop
screen.new("DU3","LCD",60,50,"controls/lighting/DU/du3"),
              screens = [screen.new("DU1","LCD",60,50,"controls/lighting/DU/du1"),
screen.new("DU4","LCD",60,50,"controls/lighting/DU/du4"),
screen.new("DU2","LCD",60,50,"controls/lighting/DU/du2"),
screen.new("DU5","LCD",60,50,"controls/lighting/DU/du5"),
screen.new("DU3","LCD",60,50,"controls/lighting/DU/du3"),
screen.new("DU6","LCD",60,50,"controls/lighting/DU/du6")];
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
      }
        },
};
</syntaxhighlight>
</syntaxhighlight>


Finally, we add the updating function: the following code is executed using a maketimer:
Finally, we add the updating function: the following code creates a maketimer and a setlistener that execute the loop(); and init(); functions respectively:
<syntaxhighlight>
<syntaxhighlight>
foreach(var screena; screens) { # note that we use a different variable, screena
var systemsLoop = maketimer(0.1, func {
power_consumption = screena.power_consumption(); # call the power_consumption() for each instance of the screen class
systems.ELEC.loop(); # runs the loop(); function every 0.1 seconds
setprop("/systems/electrical/DU/" ~ screena.name ~ "/watts",power_consumption); # write the result to a property
});
}
 
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();
});
</syntaxhighlight>
</syntaxhighlight>




These are the basic parts of OOP.
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.

Revision as of 13:51, 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:

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.