Howto:Basic OOP Programming: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
No edit summary
No edit summary
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();
   ELEC.init();
   timer.start();
   timer.start();
});
});
Line 75: Line 75:




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.
These are the basic parts of OOP. This code should be reasonable standalone, in that it should work without depending on other code. For instance, as a test, you could set prop the DU dimming properties at the beginning of the file to reasonable values, as follows:
 
<syntaxhighlight lang="nasal">
setprop("controls/lighting/DU/du1", 0.5);
setprop("controls/lighting/DU/du2", 0.6);
setprop("controls/lighting/DU/du3", 0.9);
setprop("controls/lighting/DU/du4", 0.1);
setprop("controls/lighting/DU/du5", 1.0);
setprop("controls/lighting/DU/du6", 0.4);
</syntaxhighlight>

Revision as of 13:55, 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. 
   ELEC.init();
   timer.start();
});


These are the basic parts of OOP. This code should be reasonable standalone, in that it should work without depending on other code. For instance, as a test, you could set prop the DU dimming properties at the beginning of the file to reasonable values, as follows:

setprop("controls/lighting/DU/du1", 0.5);
setprop("controls/lighting/DU/du2", 0.6);
setprop("controls/lighting/DU/du3", 0.9);
setprop("controls/lighting/DU/du4", 0.1);
setprop("controls/lighting/DU/du5", 1.0);
setprop("controls/lighting/DU/du6", 0.4);