Newbie question about how to set a variable in the sim

I’m sure it’s been asked a thousand times. I’ve tried searching but maybe I’m not using the right keywords. Is there a tutorial or code sample that explains how to set a variable in the sim with sim connect ?

Basically I’m making a simple program that reads input from a midi device and sends it to the sim via SimConnect. I’m using C++. I’d like to send input, for example autopilot heading angle, to update the plane’s autopilot heading bug.

I tried reading function references but I’m a bit confused as it doesn’t seem to be a simple thing to do. Could someone please point me in the right direction on where to get information on how this should be done?

This example is straightforward: Sending input | FSDeveloper

That code does gets and puts.

Probably a tutorial around somewhere, but basically its the SimConnect_SetDataOnSimObject function that you are looking for. Use the same data definitions as for reading.

I’ve written a simple project in C# that should be fairly easy to convert to C++.

https://forums.flightsimulator.com/t/using-the-simconnect-sdk-a-simvar-request-handler

Receiving (get) and sending (set) simulation variables such as the altitude, longitute, speed etc. involves the following:

Once you have an open (valid) connection established (with SimConnect_Open)

  • Define the data structure (= record) of the data you want to receive / send → SimConnect_AddToDataDefinition
  • Receive the data, referring to your data structure → SimConnect_RequestDataOnSimObject
  • Note the Period parameter which lets you define the “frequency” by which you want to receive the data, as defined in the SIMCONNECT_PERIOD enumeration (e.g. SIMCONNECT_PERIOD_SIM_FRAME for every “frame” or SIMCONNECT_PERIOD_SECOND for every second etc. you can also temporarily disable receiving data, by setting the Period to SIMCONNECT_PERIOD_NEVER)
  • Send the data, referring again to your data structure → SimConnect_SetDataOnSimObject

You can send the data whenever you see appropriate. For receiving data you typically either setup some kind of “polling loop” (driven e.g. by some timer, or an endless loop with some “sleep” interval, as many of the SDK examples do…), or a “win32 event based callback”. There is a discussion about this here in the forum.

For the polling approach you need to call the “process data callback” yourself (somewhat ironically, as the name “callback” actually implies that your function should be automatically be “called back” → but apparently that is indeed the case when you register via the “win32 event queue”, but which also requires a valid HWND “window handle”, refer again to SimConnect_Open - otherwise you can safely pass a nullptr for the HWND parameter)

In either case, for receiving data (including “system events”, to which you may want to “subscribe” separately as well, see SimConnect_SubscribeToSystemEvent, if needed) you need to specify a dispatch function, like

void CALLBACK SkyConnectImpl::sampleDataCallback(SIMCONNECT_RECV *receivedData, DWORD cbData, void *context)

Take note of the context parameter, with which you can provide any “context” data you want: typically the “this”-pointer for classes which implement the SimConnect functionality, so you have access to all your members of your class, from within your callback (“dispatch”) implementation.

Then you determine based on the type of the received data what kind of data (or event) you just received, and deal with it accordingly. For instance the resulting data (as a result of your above SimConnect_RequestDataOnSimObject call) can be accessed as follows:

switch(receivedData->dwID)
{
    ...
    case SIMCONNECT_RECV_ID_SIMOBJECT_DATA:
        objectData = reinterpret_cast<SIMCONNECT_RECV_SIMOBJECT_DATA *>(receivedData);

        switch(objectData->dwRequestID)
        {
            case AircraftPositionRequest:
            {
                // Process/store your result data from the "AircraftPositionRequest" here; you can use
                // the context input parameter for the "this"-pointer of your class, to get access to its members
                ...
                break;
            }
            ...

Where does AircraftPositionRequest come from again? That is an enumeration value (name of your choice) which you also used in your initial SimConnect_RequestDataOnSimObject request, e.g.

enum DataRequest {
    AircraftInfoRequest,
    AircraftPositionRequest
};

And finally, if you go for a “polling approach”, you need to periodically call the SimConnect_CallDispatch function, in order for your callback function to actually be called (and you need to pass the callback function as parameter here).

You find the full examle here:

https://github.com/till213/SkyDolly/blob/main/src/SkyConnect/src/SkyConnectImpl.cpp

:wink:

UPDATE: Oh, and quite important as well, here are the actual simulation variables that you can get and set (most of them anway, but not all): Simulation Variables