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

Sending input

Messages
2
Country
unitedstates
I have been working with SimConnect for a bit now and have everything OUT of it that I want for the moment. My current issue is getting things back INTO the game i.e inputs. The values I am interested in are not currently settable according to the documentation (interested in autopilot altitude / airspeed settings). I figured for now I would just bind the relevant things to unused keys and simulate keypresses, however I have as of yet been unable to get the game to accept any simulated key press events. I have tried various approaches such as PostMessage and SendKeys, I suspect that it is doing some kind of low level hardware polling or something. I am using C# for my current testing. Wondering if anyone has had any luck getting input back into the game in any way?
 
Messages
87
Country
russia
Read SDK, study examples!
C++:
#include <Windows.h>
#include <cstdio>
#include "SimConnect.h"

HANDLE hSimConnect = NULL;

enum DATA_DEFINE_ID
{
    DEFINITION_ID_AP,
};

enum DATA_REQUEST_ID
{
    REQUEST_AP_SETTINGS,
};

enum EVENT_ID
{
    EVENT_SET_AP_ALTITUDE,
};

struct AutopilotData
{
    double available;
    double altitude_tunable;
    double altitude_feet;
};

int main()
{
    HRESULT hr;
    SIMCONNECT_RECV* pData = NULL;
    DWORD cbData = 0;
    bool bRequestProcessed = false;
    int SelectedAltitude = 0;
    SIMCONNECT_RECV_SIMOBJECT_DATA* pObjData = NULL;
    AutopilotData* pAutopilotData = NULL;
    
    // Connect to MSFS
    if (FAILED(SimConnect_Open(&hSimConnect, "AP changer", NULL, 0, 0, 0)))
    {
        printf("Unable to connect to MSFS using SimConnect\n");
        exit(EXIT_FAILURE);
    }
    printf("Connected to MSFS!\n");

    // Request current autopilot settings
    hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ID_AP, "AUTOPILOT AVAILABLE", "Bool");
    if (FAILED(hr))
    {
        printf("AddToDataDefinition for AUTOPILOT AVAILABLE - error\n");
    }
    hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ID_AP, "AUTOPILOT ALTITUDE MANUALLY TUNABLE", "Bool");
    if (FAILED(hr))
    {
        printf("AddToDataDefinition for AUTOPILOT ALTITUDE MANUALLY TUNABLE - error\n");
    }
    hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ID_AP, "AUTOPILOT ALTITUDE LOCK VAR", "Feet");
    if (FAILED(hr))
    {
        printf("AddToDataDefinition for AUTOPILOT ALTITUDE LOCK VAR - error\n");
    }

    hr = SimConnect_RequestDataOnSimObject(hSimConnect, REQUEST_AP_SETTINGS, DEFINITION_ID_AP, SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD_ONCE);
    if (FAILED(hr))
    {
        printf("RequestDataOnSimObject for AutopilotData structure - error\n");
    }
    bRequestProcessed = false;
    while (!bRequestProcessed)
    {
        hr = SimConnect_GetNextDispatch(hSimConnect, &pData, &cbData);
        if (SUCCEEDED(hr))
        {
            pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData;
            if (pData->dwID == SIMCONNECT_RECV_ID_SIMOBJECT_DATA && pObjData->dwRequestID == REQUEST_AP_SETTINGS)
            {
                bRequestProcessed = true;
                pAutopilotData = (AutopilotData*)&pObjData->dwData;
                if (pAutopilotData->available == 0)
                    printf("Your airplane doesn't have autopilot, sorry.\n");
                if (pAutopilotData->altitude_tunable > 0)
                    printf("This SimObject can accept new autopilot altitude value.\n");
                else
                    printf("This SimObject can't accept new autopilot altitude value, sorry.\n");
                printf("Current autopilot selected altitude: %.0f feet\n", pAutopilotData->altitude_feet);
            }
        }
    }

    // Ask user for new value for altitude
    printf("What altitude do you want? ");
    if (scanf_s("%d", &SelectedAltitude) != 1 || SelectedAltitude < 0)
    {
        printf("Can't set you altitude. Will set 0.\n");
        SelectedAltitude = 0;
    }

    // Set new values
    hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_SET_AP_ALTITUDE, "AP_ALT_VAR_SET_ENGLISH");
    if (FAILED(hr))
    {
        printf("MapClientEvetnToSimEvent for AP_ALT_VAR_SET_ENGLISH - error\n");
    }
    hr = SimConnect_TransmitClientEvent(hSimConnect, SIMCONNECT_OBJECT_ID_USER, EVENT_SET_AP_ALTITUDE, SelectedAltitude, SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
    if (FAILED(hr))
    {
        printf("TransmitClientEvent for SelectedAltitude value - error\n");
    }

    // Check current altitude setting
    hr = SimConnect_RequestDataOnSimObject(hSimConnect, REQUEST_AP_SETTINGS, DEFINITION_ID_AP, SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD_ONCE);
    if (FAILED(hr))
    {
        printf("RequestDataOnSimObject for AutopilotData structure - error\n");
    }
    bRequestProcessed = false;
    while (!bRequestProcessed)
    {
        hr = SimConnect_GetNextDispatch(hSimConnect, &pData, &cbData);
        if (SUCCEEDED(hr))
        {
            pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData;
            if (pData->dwID == SIMCONNECT_RECV_ID_SIMOBJECT_DATA && pObjData->dwRequestID == REQUEST_AP_SETTINGS)
            {
                bRequestProcessed = true;
                pAutopilotData = (AutopilotData*)&pObjData->dwData;
                printf("Current autopilot selected altitude: %.0f feet\n", pAutopilotData->altitude_feet);
            }
        }
    }

    // Close
    hr = SimConnect_Close(hSimConnect);


    return 0;
}
Screenshot 2020-10-09 004748.png
 
Last edited:
Messages
2
Country
unitedstates
Awesome, thanks for the sample code and feedback. Just curious is that something you just whipped up or can that be found somewhere in the SDK (I don't see anything similar at first glance).

Looks like my issue was looking at and trying to do this via SimVar's and not Events.
 
Messages
87
Country
russia
Examples of all SimConnect functions can be found in SDK (not completed programs, just simple highlights of API using).
In section Working samples there are many small programs, that demonstrate using SimConnect in most common situations. That section and examples folder not included in MSFS SDK 0.6.0. But most (but not all) SimConnect examples from FSX SDK or Prepar3D SDK are the same.
I just suggest that something broke down after many similar questions on forum. Also in 0.6.0 they state that "The mapping between Input and Sim events is only partially supported at the moment" so I want to double check that.
 
Messages
26
Country
unitedstates
Examples of all SimConnect functions can be found in SDK (not completed programs, just simple highlights of API using).
In section Working samples there are many small programs, that demonstrate using SimConnect in most common situations. That section and examples folder not included in MSFS SDK 0.6.0. But most (but not all) SimConnect examples from FSX SDK or Prepar3D SDK are the same.
I just suggest that something broke down after many similar questions on forum. Also in 0.6.0 they state that "The mapping between Input and Sim events is only partially supported at the moment" so I want to double check that.
Ive been looking through the sdk examles, but I just cant rap my head around it. I am trying to create a custom input, to set the RECIP ENG MANIFOLD PRESSURE, but of course it is not in the Event IDs. My question is, how do I define in MapClientEventToSimEvent, that my Event is going to be RECIP ENG MANIFOLD PRESSURE?
 
Messages
26
Country
unitedstates
You may use SimConnect_SetDataOnSimObject function for simulation variables that can be settable. Look at this example from Prepar3D SDK ThrottleControl.cpp
I guess im not really understanding the SimConnect_SetDataOnSimObject function. Im not making the link to where you put what you would want the simvar to be set to. so for example, if I want the throttles to go to 100% if im above 3,500 feet instead of having to press a button to do the function, then how would I put the input of 100% into SimConnect_SetDataOnSimObject? Ive been playing around with it for a couple of hours now and cant seem to find the correct way, even with the throttlecontrol.cpp example, and SetData.cpp example
 
Messages
25
Country
unitedstates
Thanks for giving us the code above, Prepar3DGuy!

I have another post in the forum in regards to getting data from the sim. Ignore that post, if you can, please. I am trying this now with C++.

When I go to compile your example above I am getting linker errors on the SimConnect calls. Here is a screen print of why it is not compiling. What am I missing from my environment? I suspect it is the SimConnect.lib not being included in the project, but you "pros" will instantly know. It has been 30 years at least since I compiled in C/C++.

FSInfo Linker Errors.png
 
Messages
87
Country
russia
First of all use x64 platform compiler for MSFS SimConnect instead of x86 in your screenshot.
The next is simple check and follow steps 1, 2, 3 of section Manual Configuration on SDK page $(MSFS_SDK)/Documentation/04-Developer_Tools/SimConnect/SimConnect_Reference.html#cc-projects
 
Messages
25
Country
unitedstates
Well, Prepar3DGuy... You made my day! Thank you so much for pointing me in the right direction, Man.

After an hour tweaking VS, re-installing MSDS SDK "with" VS installed. I got it to compile and link. I also had to watch a YouTube video (Spanish!) on how to find the Property Manager window. But it's all good now! I was able to see the SimConnect "ALT" Altitude setting and the rest will just take learning the other objects to get the data I need. I removed the input section of your code, that's why my console output looks different.

Thanks again, Mate!

Excellent.jpg



This video helped too:
 
Messages
25
Country
unitedstates
A quick question for you pro developers: I am getting my sim-variables from the sim with no issues. Is there a call that I can make to see the status of my connection with SimConnect/MSFS?

I need to know if the user has gone back to the main menu or has completely unloaded MSFS.

SimConnect_RequestSystemState()?

Thanks for your response!
 
Messages
87
Country
russia
Yes, SimConnect_RequestSystemState() will be fine to request state once at a current moment. If you need to subscribe to simulator events and want to process such event when it occurs I recommend to use SimConnect_SubscribeToSystemEvent() function instead.
About connection status of your SimConnect client and MSFS. It's alive after successfully SimConnect_Open (actually after receiving SIMCONNECT_RECV_ID_OPEN) and before calling SimConnect_Close. If you want to be aware of the situation when MSFS will close (starting to exit) before your client, then process SIMCONNECT_RECV_ID_QUIT in YourDispatchProc callback function and do appropriately.
 
Last edited:
Messages
92
Country
unitedstates
What is Prepar3D ? I read the intro but not sure , is it an add on to the MSFS SDK? Is there a SimConnect in all flight simulators? Or is this SimConnect referencing MSFS? Can you point to me an example returning a string variable (MSFS 2020 SDK C#?)
 
Messages
87
Country
russia
What is Prepar3D ?
Another simulation platform Prepar3D that based on Microsoft ESP and evolution in the last 10 years for commercial simulation.
is it an add on to the MSFS SDK?
No, it is separate products with different purpose and target audience.
Is there a SimConnect in all flight simulators? Or is this SimConnect referencing MSFS?
No. SimConnect is available for FSX, Prepar3D and MSFS. It has the same idea for interprocess communication as client/server architecture, but some functionality has extended differently from some point. Now it's mostly incompatible version of the same interface. But idea is the same and simple examples works the same. (need to be compiled with appropriate version).
 
Messages
25
Country
unitedstates
Yes, SimConnect_RequestSystemState() will be fine to request state once at a current moment. If you need to subscribe to simulator events and want to process such event when it occurs I recommend to use SimConnect_SubscribeToSystemEvent() function instead.
About connection status of your SimConnect client and MSFS. It's alive after successfully SimConnect_Open (actually after receiving SIMCONNECT_RECV_ID_OPEN) and before calling SimConnect_Close. If you want to be aware of the situation when MSFS will close (starting to exit) before your client, then process SIMCONNECT_RECV_ID_QUIT in YourDispatchProc callback function and do appropriately.
Once again, PDGuy, Thanks!

I will read-up on how SimConnect_SubscribeToSystemEvent() works and will ask questions if needed. That call maybe somewhere in the samples too and I will look there as well.

When I land on Mars, I will be sure to save a fresh, clean suit for ya! :)
 
Top