• 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 Proper way to detect variable changes

Messages
22
Hey guys, I'm coding a piece of software for my home made cockpit.
What I would like to know is what is the proper or more efficient way to detect when a variable changes its value.
This is an example about how I do it right now:

Code:
void processData(Data *pS)
{
        if(pS->myVar) // boolean
        {
                         // do something
        }else{
                         // do something
        }
}

Even when it works, there is a ton of variables I must check on every cycle, I thing there must be a better way to handle this, right?
 
Surely the only way to detect if a variable has changed is to check that the variable itself has changed?
 
What do you expect such an API to achieve? You could check the SimConnect API in the SDK SimConnect API Reference.
 
You want something like this:
hr = SimConnect_RequestDataOnSimObject( hSimConnect, A.ID+6000, DATA_DEFINITION_AIRCRAFT_DATA,A.ID, SIMCONNECT_PERIOD_SECOND, SIMCONNECT_DATA_REQUEST_FLAG_CHANGED , NULL, 4, NULL );
 
You want something like this:
hr = SimConnect_RequestDataOnSimObject( hSimConnect, A.ID+6000, DATA_DEFINITION_AIRCRAFT_DATA,A.ID, SIMCONNECT_PERIOD_SECOND, SIMCONNECT_DATA_REQUEST_FLAG_CHANGED , NULL, 4, NULL );

Thank you Mr Dawson, I will start reading there.
Your help is always invaluable.
 
If the SIMCONNECT_DATA_REQUEST_FLAG_CHANGED flag is set in SimConnect_RequestDataOnSimObject() then the SDK reads
"Data will only be sent to the client when one or more values have changed. If this is the only flag set, then all the variables in a data definition will be returned if just one of the values changes."
If a variable is a float/double then it could change in the last decimal point without there being a meaningful change.
 
You could bulid an array with all of the AVars you want to check, then use a loop for testing each one and see which has changed.

Tom
 
If the SIMCONNECT_DATA_REQUEST_FLAG_CHANGED flag is set in SimConnect_RequestDataOnSimObject() then the SDK reads
If a variable is a float/double then it could change in the last decimal point without there being a meaningful change.

Which is why, the SimConnect_AddToClientDataDefinition, which must be called before requesting any data, has the optional fEpsilon parameter:

fEpsilon
[in, optional] If data is requested only when it changes, a change will only be reported if it is greater than the value of this parameter (not greater than or equal to). The default is zero, so even the tiniest change will initiate the transmission of data. Set this value appropriately so insignificant changes are not transmitted. This can be used with integer data, the floating point fEpsilon value is first truncated to its integer component before the comparison is made (for example, an fEpsilon value of 2.9 truncates to 2, so a data change of 2 will not trigger a transmission, and a change of 3 will do so).
 
Back
Top