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

MSFS20 Trying to figure out the best way to communicate between on-screen in-game panels and SimConnect

Messages
83
Country
us-newyork
I have an in-game panel I created (basically a HTML/JS panel, same as the top menu panels like Weather, Camera, etc.), and I'd like to be able to send an event to SimConnect (which will trap it and act on it) when, for instance, I toggle a switch or press a button. Right now I am using LVARS but I find them unreliable - sometimes the LVAR is not passed through to the SimConnect client.

Is there a better way?
 
I'm not sure if it'll work (I've never tried this in particular), but you might be able to use SimConnect_MapClientEventToSimEvent if the only thing you want to communicate is whether something happened (using the Custom.Event or #0xnnnn stuff in the EventName parameter)? (I'm not sure if Coherent will let you send those sorts of custom events on the JavaScript side though.)

Otherwise, your best bet is probably the Communication API (though if you have an external/out-of-process SimConnect client, that's only an option in MSFS 2024 with the new SimConnect Communication API as of Sim Update 5 -- but if you have a WASM module, you can use the Communication API regardless; the Communication API is available through WASM in both MSFS 2020 and MSFS 2024).
 
I'm not sure if it'll work (I've never tried this in particular), but you might be able to use SimConnect_MapClientEventToSimEvent if the only thing you want to communicate is whether something happened (using the Custom.Event or #0xnnnn stuff in the EventName parameter)? (I'm not sure if Coherent will let you send those sorts of custom events on the JavaScript side though.)

Otherwise, your best bet is probably the Communication API (though if you have an external/out-of-process SimConnect client, that's only an option in MSFS 2024 with the new SimConnect Communication API as of Sim Update 5 -- but if you have a WASM module, you can use the Communication API regardless; the Communication API is available through WASM in both MSFS 2020 and MSFS 2024).
Hey, THANKS for taking time to answer, I appreciate it! I was Googling it in the last hour or so and it spewed out something similar that I may try (tomorrow, very sleepy now), but this is what AI dished out:

On JS side:

Code:
// Example: Trigger a custom event without a parameter (e.g., Increase Heading)
SimVar.SetSimVarValue("K:A32NX.FCU_HDG_INC", "number", 0);
// Example: Trigger an event requiring a specific value/axis state
SimVar.SetSimVarValue("K:SOME_CUSTOM_EVENT", "number", 1);


And on C++ side:

Code:
#include <windows.h>
#include "SimConnect.h"

// Define your custom event enum ID
enum EVENT_ID {
    EVENT_CUSTOM_K_VAR
};

// Map the custom event string to your internal ID
SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_CUSTOM_K_VAR, "A32NX.FCU_HDG_INC");

// Transmit the event (dwData represents the value/state)
DWORD dwData = 0; // Value parameter, if applicable
SimConnect_TransmitClientEvent(
    hSimConnect,
    0, // Object ID (0 for user aircraft)
    EVENT_CUSTOM_K_VAR,
    dwData,
    SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
    SIMCONNECT_GROUP_PRIORITY_HIGHEST
);

I'll report how this works out.
 
For what it's worth (just in case it isn't clear), if you're dealing with key events and want to listen on the SimConnect side, you'll still need to use SimConnect_AddClientEventToNotificationGroup (and possibly SimConnect_SetNotificationGroupPriority as well) at some point -- it's worth looking at the SDK samples for reference (LLMs only ever create things that plausibly sound like answers, but there's no guarantee that it'll necessarily be an actual answer, especially for niche stuff like SimConnect).
 
For what it's worth (just in case it isn't clear), if you're dealing with key events and want to listen on the SimConnect side, you'll still need to use SimConnect_AddClientEventToNotificationGroup (and possibly SimConnect_SetNotificationGroupPriority as well) at some point -- it's worth looking at the SDK samples for reference (LLMs only ever create things that plausibly sound like answers, but there's no guarantee that it'll necessarily be an actual answer, especially for niche stuff like SimConnect).
Hey, thanks! The problem is, there ARE no SDK samples for this particular case, unless I am missing something...

Ok, I implemented my code, but no dice :(

Here is the code, first, setup on SimConnect/C++ side:
Code:
hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_CUSTOM_K_VAR, "CUSTOM_K_VAR");
hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP_KEYS, EVENT_CUSTOM_K_VAR);
hr = SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP_KEYS, SIMCONNECT_GROUP_PRIORITY_HIGHEST);

And then, in JS, where the event is triggered:
Code:
onToggle_MY_TOGGLE() {
        if (this.m_toggle_MY_TOGGLE.toggled) {
            SimVar.SetSimVarValue("K:CUSTOM_K_VAR", "number", 0);
        }
    }
And finally, in the event loop, where the event should be trapped:
Code:
case SIMCONNECT_RECV_ID_EVENT:
            {
                SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
                switch(evt->uEventID)
                {
                    case EVENT_CUSTOM_K_VAR:
                        printf("EVENT_CUSTOM_K_VAR WORKING\n");
                        break;
No event is being trapped :rolleyes:
 
I'd recommend trying what the documentation says about custom events (you can't just pick whatever you want for the EventName parameter; it needs to follow the convention as described in the SDK):

2024: https://docs.flightsimulator.com/ms..._Data/SimConnect_MapClientEventToSimEvent.htm
2020: https://docs.flightsimulator.com/ht..._Data/SimConnect_MapClientEventToSimEvent.htm
FSX/ESP: https://learn.microsoft.com/en-us/p...=msdn.10)#simconnect_mapclienteventtosimevent

Refer to the Event IDs document for a list of event names (listed under String Name). If the event name includes one or more periods (such as "Custom.Event" in the example below) then they are custom events specified by the client, and will only be recognized by another client (and not ESP) that has been coded to receive such events. No ESP events include periods. If no entry is made for this parameter, the event is private to the client.

Alternatively enter a decimal number in the format "#nnnn" or a hex number in the format "#0xnnnn", where these numbers are in the range THIRD_PARTY_EVENT_ID_MIN and THIRD_PARTY_EVENT_ID_MAX, in order to receive events from third-party add-ons to ESP.

Particularly, look at the SendEventA, SendEventB, SendEventC, or SetData samples. I'm still not sure if you'll be able to send events from Coherent, but that should show you how custom events work on the SimConnect side.
 
Hey, thanks! The problem is, there ARE no SDK samples for this particular case, unless I am missing something...

Ok, I implemented my code, but no dice :(

Here is the code, first, setup on SimConnect/C++ side:
Code:
hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_CUSTOM_K_VAR, "CUSTOM_K_VAR");
hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP_KEYS, EVENT_CUSTOM_K_VAR);
hr = SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP_KEYS, SIMCONNECT_GROUP_PRIORITY_HIGHEST);

And then, in JS, where the event is triggered:
Code:
onToggle_MY_TOGGLE() {
        if (this.m_toggle_MY_TOGGLE.toggled) {
            SimVar.SetSimVarValue("K:CUSTOM_K_VAR", "number", 0);
        }
    }
And finally, in the event loop, where the event should be trapped:
Code:
case SIMCONNECT_RECV_ID_EVENT:
            {
                SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
                switch(evt->uEventID)
                {
                    case EVENT_CUSTOM_K_VAR:
                        printf("EVENT_CUSTOM_K_VAR WORKING\n");
                        break;
No event is being trapped :rolleyes:
Hi,

You are very close... all you need to do is rename your custom var. It really does need a "." in the name - all you need to do is insert "something." in front of your variable (in our case below, "MY."). So:
Code:
hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_CUSTOM_K_VAR, "MY.CUSTOM_K_VAR");
hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP_KEYS, EVENT_CUSTOM_K_VAR);
hr = SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP_KEYS, SIMCONNECT_GROUP_PRIORITY_HIGHEST);

Then:
Code:
onToggle_MY_TOGGLE() {
        if (this.m_toggle_MY_TOGGLE.toggled) {
            SimVar.SetSimVarValue("K:MY.CUSTOM_K_VAR", "number", 0);
        }
    }
... and your event loop should trap that.

You can even send parameters (data) in JS, and that will be picked up in SimConnect:
Code:
onToggle_MY_TOGGLE() {
        if (this.m_toggle_MY_TOGGLE.toggled) {
            SimVar.SetSimVarValue("K:MY.CUSTOM_K_VAR", "number", 1);
        } else
       {
            SimVar.SetSimVarValue("K:MY.CUSTOM_K_VAR", "number", 0);
        }
    }

And trapping it:
Code:
case SIMCONNECT_RECV_ID_EVENT:
            {
                SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
                switch(evt->uEventID)
                {
                    case EVENT_CUSTOM_K_VAR:
                        TB_PRINTF("EVENT_CUSTOM_K_VAR WORKING: %d\n", (int)evt->dwData);
                        break;

And now you are getting the data passed in JS! "MY." can of course be anything, usually devs use it to designate company and product, as in "PMDG737.CUSTOM_K_VAR" so that it is not mixed up with other client's events.
 
Back
Top