• 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.

(fixed) c++ simconnect overriding sim provided simvars

Messages
27
Country
unitedstates
Attempting to set RECIP ENG MANIFOLD PRESSURE:1 , but it simply is not setting, even though the debug cout<< is running

I can see that the SimConnect_SetDataOnSimObject function is working, so the problem has to be from another part of the code no? If someone can help me find the issue that would be greatly appreciated.

C++:
enum DATA_DEFINE_ID
{
DEFINITION_MANIFOLD,
};

enum DATA_REQUEST_ID
{
REQUEST_AP_SETTINGS,
REQUEST_MANIFOLD,
};
/*
 * ========================== *
 * SimVars via non-SimConnect *
 * ========================== *
 */
struct SIM_VARS
{
    // Units
    ENUM Psi; // Bool
    ENUM feet_units; // Feet
    // Variables
    ENUM recip_manifold_Psi; // ELEVATOR DEFLECTION
    ENUM indicated_altitude;// SIM ON GROUND
} sim_vars;

struct structManifoldControl
{
double manifoldPressure;
}manifoldqnh;

structManifoldControl tc;

bool HandleSimVars(int service_id)
{
    switch (service_id)
    {
    case PANEL_SERVICE_PRE_INSTALL:
    {
        
        return true;
    }
    break;
    case PANEL_SERVICE_POST_INSTALL:
    {
        // Units
        sim_vars.Psi = get_units_enum("Psi");
        sim_vars.feet_units = get_units_enum("Feet");

        // Variables
        sim_vars.recip_manifold_Psi = get_aircraft_var_enum("RECIP ENG MANIFOLD PRESSURE:1");
        sim_vars.indicated_altitude = get_aircraft_var_enum("INDICATED ALTITUDE");
        return true;
    }
    break;
    case PANEL_SERVICE_PRE_DRAW:
    {
        return true;
    }
    break;
    case PANEL_SERVICE_PRE_KILL:
    {
        return true;
    }
    break;
    }
    return false;

}




bool TestVars(FsContext ctx, int service_id, void* pData) {
    switch (service_id)
    {
    case PANEL_SERVICE_PRE_INSTALL:
    {

        return true;
    }
    break;
    case PANEL_SERVICE_POST_INSTALL:
    {   

        if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Client Event", NULL, NULL, NULL, NULL))) {
            std::cout << "Connected to SimConnect" << endl;
        }
        else {
            std::cout << " Failed to connect to SimConnect" << endl;
        }
        SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_MANIFOLD, "RECIP ENG MANIFOLD PRESSURE:1", "Psi");
        return true;
    }
    break;


    case PANEL_SERVICE_PRE_DRAW:
    {

        auto recipmanifold = aircraft_varget(sim_vars.recip_manifold_Psi, sim_vars.Psi, 0);
        double indicated = aircraft_varget(sim_vars.indicated_altitude, sim_vars.feet_units, 0);

        if (indicated > 1500) {

            HRESULT hr;
            
            std::cout << "above 1500" << std::endl;

            tc.manifoldPressure =  29;
            
            hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_MANIFOLD, SIMCONNECT_OBJECT_ID_USER, NULL, 0, sizeof(tc), &tc);
            if (SUCCEEDED(hr)) {


                cout << "Set Data is working" << endl;
            }
            else
            {
                cout << "Set Data may not be working" << endl;
            }
        }

        return true;
    }
    break;
    case PANEL_SERVICE_PRE_KILL:
    {
        if (hSimConnect)
        {
            SimConnect_Close(hSimConnect);
            hSimConnect = 0;
        }
    }
    break;
    }

    return true;
}



/*
    * ============ *
    * Finalization *
    * ============ *
    */
extern "C" {

    MSFS_CALLBACK bool TurboCharger_gauge_callback(FsContext ctx, int service_id, void* pData)
    {
        auto ret = true;

        ret &= HandleSimVars(service_id);
        ret &= TestVars(ctx, service_id, pData);
        //ret &= HandleSimConnect(ctx, service_id, pData);
        return ret;
    }
}
 
The sim is probably overwriting your input from it's own engine simulation.

Also, not sure the PRE_DRAW is the correct timing.
 
PRE_DRAW is not the phase at which data is changed. Of course, that's with FSX and Prepar3D. Who knows what Asobo has done. Most of the SImConnect stuff doesn't work correctly yet. Especially the writing.
 
PRE_DRAW is not the phase at which data is changed. Of course, that's with FSX and Prepar3D. Who knows what Asobo has done. Most of the SImConnect stuff doesn't work correctly yet. Especially the writing.
I finally figured out the issue. Like you said, the sim writes its own data, but I found a loop hole to fix that. If you find the difference between the sim written value and the value you want to set that will allow you to take over the sim written input. By doing that we can use simple math to set our wanted value. Here's what I came up with.

C++:
            //Gets the diffrence between the FDE and the wanted value
            double preSet = recipmanifold - 29.00;
            //Make the difference 0 by subtracting by its self ( more than likely always going to be a negitve #
            double mathSet = preSet - preSet;
            //Add mathSet which is = 0 by the number I want
            double finalSet = mathSet + 29.00;

            //Send the number wanted refrenced by the number added to finalSet
            tc.manifoldPressure =  finalSet;


            
            hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_MANIFOLD, SIMCONNECT_OBJECT_ID_USER, NULL, 0, sizeof(tc), &tc);
 
Don't rely on it. At some point Asobo are quite likely to release an SDK that 'fixes' the loophole that you are exploiting.
 
Don't rely on it. At some point Asobo are quite likely to release an SDK that 'fixes' the loophole that you are exploiting.
For sure, w all hope they fix a lot of the old simconnect stuff that just doesn't make a bit of sense, but for now this would do
 
Just a side note... PRE_DRAW is called at the frame rate (as it's the code a gauge executes to provide a gauge image for the sim)... which means your code in the PRE_DRAW affects the framerate. It should be minimal, streamlined and fast. At 60fps, your code would have to execute below 16ms (assuming it was the only code being called for the PRE_DRAW cycle).
 
Just a side note... PRE_DRAW is called at the frame rate (as it's the code a gauge executes to provide a gauge image for the sim)... which means your code in the PRE_DRAW affects the framerate. It should be minimal, streamlined and fast. At 60fps, your code would have to execute below 16ms (assuming it was the only code being called for the PRE_DRAW cycle).
Which phase should be used instead in this type of example? Pre/Post_update?
 
PRE_UPDATE. If you want to know which case statements are called when, download sd2gau38.zip "C-Language Gauge Creation" and read the section on "Gauge Callback Case Statements". Watch out for the nasty: during a window resize PANEL_SERVICE_CONNECT_TO_WINDOW executes before PANEL_SERVICE_DISCONNECT. If you use PANEL_SERVICE_CONNECT_TO_WINDOW to initialise SimConnect then SimConnect gets assigned an
additional handle at this point. This doesn't always kill SimConnect but can start a memory leak which will eventually crash the sim.
 
Last edited:
Back
Top