However, I disagree with his comment telling that execute_calculator_code call is inefficient, because efficiency in code programming is a very subjective word.
There's nothing subjective here. The
execute_calculator_code IS less efficient than the aircraft_varget for the following reasons:
- You don't have to call XML expression parser. The thing that turns the string passed to
execute_calculator_code into the code that reads the airplane variable. The aircraft_varget starts already on the next step and skip the XML text parsing operation.
- By using numbered tokens, you save ANOTHER step, which is the lookup table on the (long) list of variables to map whatever variable name into its actual number. FSX itself stores all the variables as indexes on a table. By getting the index only ONCE, outside the update loop, you save even this step. And it's not just the variable names which are indexed, the units of measure are indexed in the same way, and they can be optimized too, as I've shown.
That's why I wrote this:
Code:
int unit_bool = get_units_enum("bool");
int token_gen_master_alt = get_aircraft_var_enum( "GENERAL ENG MASTER ALTERNATOR" );
To be OUTSIDE the UPDATE cycle, with this code inside the UPDATE cycle:
Code:
gen_master_alt_status = aircraft_varget( token_gen_master_alt, unit_bool, 0 )
Instead of the slower (although nicer to look at) version:
Code:
gen_master_alt_status = aircraft_varget( get_aircraft_var_enum( "GENERAL ENG MASTER ALTERNATOR" ), get_units_enum("bool"), 0 )
It is true that using execute_calculator_code to retrieve script data is not as fast as using direct methods, but that difference, if the function is not abused, would be insignificant.
If you are working on an airplane project, chances are you will have to read MANY variables. Which means, the inefficiency of the
execute_calculator_code will be more and more of an issue, the more complex your code will become. Instead, the pure C++ approach would SCALE WELL and of course it doesn't impose any penalty for smaller projects. That's why it's best to use the pure C++ approach: it's way better for larger projects and it's surely not worse for smaller ones.
On top of that, there was the other issue, which is polling 18 times per second for a variable that is very unlikely will ever change with such high frequency, and this combined with the inefficiency of the
execute_calculator_code , forced my mental "inefficient code red light" to be turned on, so I had to comment...
I for one will use both methods, -execute_calculator_code and varget- depending on the situation; both are technically correct. As I use to say, in the end it should depend on which one the programmer feels more comfortable with.
I never said the
execute_calculator_code should NEVER be used. In fact, if EVENT HANDLERS are used, the impact of the interpreted code in this case would have been negligible, but surely it shouldn't be used to read something that changes constantly, such airspeed, altitude, etc. and that *requires* to be put in the UPDATE cycle. Inside your UPDATE cycle, you want to be as tight as possible.
There are some cases where the
execute_calculator_code MUST be used.
For example, if you need to read a P: or an E: variable, that are not available using the
aircraft_varget function which, as the name implies, reads only A: variables which, incidentally, are the most commonly used!
That's another reason why it's best to use it, the golden rule of optimization is to work on the most common case first, then worry about the rest if you have time. Who cares if a read of a P: or E: variable is slower, you won't have so many of them in the typical airplane project anyway...
Even custom L: variables can be used WITHOUT the
execute_calculator_code , by using the register_named_variable and related functions, explained here:
http://www.fsdeveloper.com/wiki/index.php?title=XML:_Variables_in_C_Gauges
Another case when you can't avoid
execute_calculator_code, is to read variables that have a STRING as their return type, since
aircraft_varget will always return a number.
Another usage case of the
execute_calculator_code is when you need to execute something that is NOT predictable by your program. For example, letting the user input an XML expression and evaluate it. An example for this is in our GSX program. The program needs to be customized by users to recognize any airplane doors status, so the user is free to type an arbitrary XML expression that would be return either 0 or 1 if that specific door is open or closed. We obviously need use
execute_calculator_code in that case, because the actual expression is read from the configuration file, it can be anything, and is not known at compile time. This is the only case where using
execute_calculator_code even for A: variables can't be avoided.