• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

FSX C++ Gauge Question

Messages
74
Country
flag
Hello all
Code:
case PANEL_SERVICE_PRE_UPDATE:
   FLOAT64 gen_mast = 0;
   execute_calculator_code ("(A:GENERAL ENG MASTER ALTERNATOR:1, Bool)", &gen_mast, NULL, NULL);
    if(gen_mast == 1)
    {
        m_Sound->PlayWaveFile0();
    }
Like you can see I have the situation above.
I need to play the sound when generator is switched off OR on. But my actual expression plays sound only when generator is ON.

Any suggestion will be appreciate =)

PS
Happy Hung... Oops! Olympic Games to all of you
 
In that case you could write something like:

Code:
case PANEL_SERVICE_PRE_UPDATE:
   FLOAT64 gen_mast = 0;
   PCSTRINGZ sScript = "(A:GENERAL ENG MASTER ALTERNATOR:1, Bool) (L:GEMA1,bool) != (A:GENERAL ENG MASTER ALTERNATOR:1,Bool) (>L:GEMA1,bool)";
   execute_calculator_code (sScript, &gen_mast, NULL, NULL);
   if(gen_mast == 1)
   {
       m_Sound->PlayWaveFile0();
    }

Notice that at startup, if generator1 is ON sound will play once cause (A:GENERAL ENG MASTER ALTERNATOR:1, Bool) and (L:GEMA1,bool) won't coincide, since LVars default to 0.

Tom
 
I might add some suggestions here:

- Put your stuff inside the _UPDATE or _DRAW cycles very sparingly and only if you don't have any other option.

- The execute_calculator_code call is inefficient.

I know that many tutorials suggest to use it, but that's just because it looks like an easy way to port some existing knowledge of XML into C++, but C++ has way better options, especially if you deal with airplane ( A: ) variables.

A the better way of doing it would be using the following functions instead.

Somewhere else (NOT in the UPDATE cycle) in your program, initialize the following variables:

Code:
int unit_bool = get_units_enum("bool");
int token_gen_master_alt = get_aircraft_var_enum( "GENERAL ENG MASTER ALTERNATOR" );
double gen_master_alt_status;

These will give you an "index" to the unit type (bool) and the token variable. It shouldn't be in the UPDATE cycle, because you don't need to have it updated 18 times per second.

In the UPDATE cycle, use this code:

Code:
// 0 is the 1st generator.
// return type for aircraft_varget is always double, will return 0 or 1 in this case

gen_master_alt_status = aircraft_varget( token_gen_master_alt, unit_bool, 0 )

This code is way more efficient because, in the time-critical update cycle you will not use the slow XML function evaluator, which will convert from XML text code to internal code 18 times per second. This method, instead, is already the direct code that talks directly with the panels system. This is the 1st optimization step.

You can do it better, NOT using an L: variable to hold the previous value, as suggested by Tom. L: variables has been designed to exchange data between panels and models, but you don't need to exchange anything here, since the intention was to play a sound when the alternator variable changes, why not just using a regular C++ variable ?

Such as:

Code:
int unit_bool = get_units_enum("bool");
int token_gen_master_alt = get_aircraft_var_enum( "GENERAL ENG MASTER ALTERNATOR" );
double gen_master_alt_status;
double gen_master_alt_previous_status;

This will be OUTSIDE the UPDATE cycle. The UPDATE cycle will look like this:

Code:
gen_master_alt_status = aircraft_varget( token_gen_master_alt, unit_bool, 0 )
if ( gen_master_alt_status != gen_master_alt_previous_status )
  {
    gen_master_alt_previous_status = gen_master_alt_status;
    m_Sound->PlayWaveFile0();
  }
This does what you asked, and it's more efficient and C/C++ correct. But we can do it even better...

I guess you goal was to generate a sound whenever the generator was pressed so, you are constantly checking ( "polling", which is usually bad ), 18 times per second, the variable that holds its status, and finally do something if that variable would ever change.

Maybe you can start seeing the issue: that variable will not likely change very often so, why checking for it 18 times per second ? Imagine having to check a very complex panel with hundreds of events that RARELY occur ? Why slowing down your sim by checking 18 times per second MANY things that never happen so frequently ? That's why polling is inherently bad: the more complex the program becomes, the more slower it will be.

The IS a better way, and of course is using HANDLERS.

An handler is a routine you write, that is called by FSX itself ( it's a "Callback" ) ONLY when something happen. If you don't do anything, your routine will never called. With an handler routine, you could intercept any FSX event AND you can also intercept custom events, happening for example when you click something in the Virtual Cockpit.

In your case, your handler routine would be called whenever you click the "thing" that controls the generators, so you know something happened, and probably could *just* play the noise, without the need for an additional control variable to check the previous status.

But in any case, the handler will be way more efficient instead of polling, because your code will only be called when something happens, NOT always, 18 times per second...

Some threads about custom handlers here:

http://www.fsdeveloper.com/forum/threads/problem-with-fsx-custom-event.20738/
http://www.fsdeveloper.com/forum/threads/custom-events-again.209647/
 
Umberto, it's timely that you mention using "Handlers" now as I've managed something similar using XML...

In a "logic script" that is contained in an XML <Update> section, I use a flag variable to detect when the "logic script" should be evaluated. At the end of the evaluation the flag is reset, thus preventing any further evaluation until the next time the flag is set. This way, switches, knobs, etc. that are rarely used are only evaluated when actually used... :scratchch

Code:
<Update>
   (L:MyFlag,bool) 1 ==
   if{
          <!-- EVALUATE THE LOGIC SCRIPT -->
          0 (>L:MyFlag,bool)
     }
   <!-- ANYTHING TO BE EVALUATED CONTIUOUSLY FOLLOWS THIS COMMENT -->
    ...................
</Update>
 
Bill,

I think Umberto was talking about real handlers, ie event handler routines. In XML would be a script written within <On Event="XX"></on>, which executes only when "something happens".

Ovbioulsy I agree with his comments regarding the use of handlers instead of continuos polling a script.

However, I disagree with his comment telling that execute_calculator_code call is inefficient, because efficiency in code programming is a very subjective word. To develop an application using one or other method/language is always a matter of trade between speed, code readability, hardware's expected limitations, user portability, and many other variables. There should be no room for "purism" seeks but instead a suitable search for a balanced result.

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.

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.

BTW, I just replied to the OP following his example, didn't mean to go deeper on that line or give him another options.


Tom
 
Thing is, when it comes to gauge design for FSX, you want to eliminate the overhead as much as possible. Its ok if you wish to read some case in the event handler which would limit it to being called when a switch moves or the like but its best to keep it out of the gauge message loop (my own term) or other high frequency loops.
 
Thank you guys. I solved it in my lazy way - just for now, after deeper understanding of FSX architecture, I swear to remake it
Code:
void LogicType::click02_mAlternator(void)
{
    FLOAT64 gen_mast = 0;
    execute_calculator_code ("(A:GENERAL ENG MASTER ALTERNATOR:1, Bool)", &gen_mast, NULL, NULL);

    if((gen_mast != gen_mast_st1) && (gen_mast == 0))
    {
        m_Sound->PlayWaveFile0();  
    }
    gen_mast_st1 = gen_mast;

    if ((gen_mast != gen_mast_st2) && (gen_mast == 1))
    {
        m_Sound->PlayWaveFile0();
    }
    gen_mast_st2 = gen_mast;
}
Umberto, you might write a book about FSX programming!
Really, man, such informative answer, I've never ever seen on FSdev. Thank you VERY much!
Grazie mille =)

Guys, have another question
Is there any way to determine a condition of the user window(I mean exterior view[F11] or VC[F9] )?

UPD
And how to totally disable "Easy start" key combination (Ctrl+E)?
Thanks
 
Last edited:
Bill,

I think Umberto was talking about real handlers, ie event handler routines. In XML would be a script written within <On Event="XX"></on>, which executes only when "something happens".

Ovbioulsy I agree with his comments regarding the use of handlers instead of continuos polling a script.
Well yes of course. I didn't intend to imply that this was the case in my example. I probably should have been more clear though about how this is used...

Each gauge cycle, the only L:var that is 'polled' is (L:MyFlag,bool). In modeldef.xml script, all switch and knob movements will, in addition to setting their own control variable, set the "flag" to 1 so that the conditional routine will become active once and thus evaluate whatever is in that section of script. Since the "flag" is reset to zero, that ends the 'polling' of anything other than the 'flag' variable again.

While this isn't even technically an "Event Handler" it does achieve the equivalent desired goal, IMHO.
 
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.
 
Last edited:
Umberto, it's timely that you mention using "Handlers" now as I've managed something similar using XML...

Well, you basically simulated an event handler with XML, by polling one variable instead of many.

While that is surely faster than polling everything, it's very likely that FSX, internally, is already doing something similar to decide if it's the case to call a Callback routine linked to a proper handler. Somewhere in the FSX code, there must be something similar to your flag, which will call a defined event handler routine only if that flag is true.

So, why duplicating what FSX already does for you (using blazing-fast compiled machine code ) and having to code a similar logic on top of it that use the XML interpreter, when the proper event handler method does everything you need ?

Your modeldef.xml will ALSO be leaner, because you won't need to do this:
In modeldef.xml script, all switch and knob movements will, in addition to setting their own control variable, set the "flag" to 1

Which, for a complex model/VC with hundreds of switches and knobs, would needlessly add to size of the .MDL file with no benefits whatsoever, when you could have just fired an event ( standard or custom using the 0x1100xxx codes directly ) and let C++ deal with it.
 
Thank you guys. I solved it in my lazy way - just for now, after deeper understanding of FSX architecture, I swear to remake it

I think that code is way more complex than it needs to be:

Code:
void LogicType::click02_mAlternator(void)
{
    FLOAT64 gen_mast = 0;
    execute_calculator_code ("(A:GENERAL ENG MASTER ALTERNATOR:1, Bool)", &gen_mast, NULL, NULL);

    if((gen_mast != gen_mast_st1) && (gen_mast == 0))
    {
        m_Sound->PlayWaveFile0();
    }
    gen_mast_st1 = gen_mast;

    if ((gen_mast != gen_mast_st2) && (gen_mast == 1))
    {
        m_Sound->PlayWaveFile0();
    }
    gen_mast_st2 = gen_mast;
}

Why you need to use TWO variables to hold the generator previous value, when they both refer to the same airplane variable and are both set to the same value ( gen_mast ) in any case ? If the intention was to play a sound only if the value has changed from the last time you checked, you would have wrote it way more clearly like this:

Code:
void LogicType::click02_mAlternator(void)
{
    FLOAT64 gen_mast = 0;
    execute_calculator_code ("(A:GENERAL ENG MASTER ALTERNATOR:1, Bool)", &gen_mast, NULL, NULL);

    if( gen_mast != gen_mast_st1)
    {
        m_Sound->PlayWaveFile0();
        gen_mast_st1 = gen_mast;
    }
}
And how to totally disable "Easy start" key combination (Ctrl+E)?

Using an event handler, of course.

You don't really "disable" it, but you can INTERCEPT the event and it's being fired by the user, and "do something" with it. For example, a VERY elegant and user friendly approach that I would really like to see, is to have an AUTOMATED, but proper, startup sequence, being performed for you, when you press the CTRL+E key. So, you can use the CTRL+E key combination (which is what a newbie user would expect) to act as a "tutorial" for a more complex startup procedure.

I don't understand why this hasn't become more standard: some developers present their airplane as not using the CTRL+E key as a "feature", to say it's realistic. But there's no need to compromise on realism: just tie the CTRL+E key to a virtual "assistant" that will perform the proper start up sequence for you, maybe even commenting it, and that would please both expert and novice users at the same time. You don't have to use it, but it might become handy for experts too, when time is short, or you simply forgot how to start that particular plane.
 
Last edited:
Well, you basically simulated an event handler with XML, by polling one variable instead of many.

While that is surely faster than polling everything, it's very likely that FSX, internally, is already doing something similar to decide if it's the case to call a Callback routine linked to a proper handler. Somewhere in the FSX code, there must be something similar to your flag, which will call a defined event handler routine only if that flag is true.
You are "preaching to the choir" Umberto. I do program most stuff in C++ for release. However, along the way one of my goals is to come up with, and describe relatively easy methods for those who only do "XML" to accomplish their work as efficiently as possible. :twocents:

BTW, the easy way to pass hex 'event ids' via LeftClick, RightClick, WheelUp, WheelDown, etc. instead of being "stuck" with <EventID> is like this:
0x11119 (>L:EVENT,enum)

Oh, and I'm glad to see you approve of the Wiki Article I wrote so many years ago... :scratchch
 
Last edited:
You are "preaching to the choir" Umberto.

I guess that's the equivalent of what we say in Italy, which sounds like "kicking in an open door" ?

However, along the way one of my goals is to come up with, and describe relatively easy methods for those who only do "XML" to accomplish their work as efficiently as possible. :twocents:

Sure, your method of polling just one variable instead of polling everything, at least shows your attention to the issue, and it's not that you have many other choices in XML, if you decided to explain something in XML.

But this wasn't an issue in this thread.

The OP initiated it by posting his own C++ code, so I assume he was looking for a C++ solution, because he already figured it out the most obvious first obstacle with Flight Sim C++ gauge development, which is setting up a compiler and have the code compiling and working correctly. And, he seems to know enough C++ ( I see classes usage in another sample he posted ) to understand my explanation.

This was a C++ thread, that's why I've tried to give the proper and most efficient C++ solution, and SkyLu seems to have appreciated it.
 
Back
Top