Hi all,
After following the SDK discussions for a time now, I like to contribute a small demo code that might help others to also understand how to directly access LVARS from outside the sim. This allows for instance to set the Airbus Nav mode (LS, LOC, NAV, ARC, MAP), or the range of the MFD. The method that I use for this, is to build a small standalone WASM (WebAssembly) that handles any EventID that is send through a SimConnect interface.
The code that allows the above mentioned LVAR binding is elaborated below. Perhaps this is trivial to some, but to me it really helps to continue binding of hardware rotaries to the FlyByWire A320 that I was not aware of how to access before (through SimConnect at least).
I don’t maintain a GitHub, so the raw code is provided below. If it is helpful, at a later stage (when I included all LVARS) I may upload the WASM directly here. Till then, when compiling this in Visual Studio, I found myself manually adding the following two VC++ include directories:
- C:\MSFS SDK\WASM\wasi-sysroot\include
- C:\Program Files (x86)\Windows Kits\10\Include
[1] Required header files.
#include <MSFS/MSFS_WindowsTypes.h>
#include <MSFS/MSFS.h>
#include <MSFS/Legacy/gauges.h>
#include <SimConnect.h>
[2] Custom event handler that interacts with the LVARs.
// Define the SimConnect custom eventHandler.
static void FSAPI EventHandler(ID32 event, UINT32 evdata, PVOID userdata) {
// Any event number from 0x11000 through 0x1FFFF is available
// but might collide with other mods.
switch (event) {
case 0x11000: {
// This sets the MFD NAV Mode.
ID idA320 = check_named_variable("A320_Neo_MFD_NAV_MODE_1");
// @TODO: range validation.
// evdata options:
// 0: LS
// 1: LOC
// 2: NAV
// 3: ARC
// 4: MAP
set_named_variable_value(idA320, evdata);
break;
}
case 0x11001: {
// This sets the MFD NAV Range
ID idA320 = check_named_variable("A320_Neo_MFD_Range_1");
// @TODO: range validation.
// evdata options:
// 0: 10 nm
// 1: 20 nm
// 2: 40 nm
// 3: 80 nm
// 4: 160 nm
// 5: 320 nm
set_named_variable_value(idA320, evdata);
break;
}
}
}
[3] Register the event handler function.
// This is called when the WASM is loaded into the system.
extern "C" MSFS_CALLBACK void module_init(void) {
register_key_event_handler((GAUGE_KEY_EVENT_HANDLER)EventHandler, NULL);
}
[4] For completeness, also unregister when done.
extern "C" MSFS_CALLBACK void module_deinit(void) {
unregister_key_event_handler((GAUGE_KEY_EVENT_HANDLER)EventHandler, NULL);
}
After compiling the WASM, it can be placed into a separate folder inside the community folder, for instance I choose
- Community/lvar-access/module/WASM_module3.wasm
You must (at least it seems) also add in the Community/lvar-access/ folder also the following:
- manifest.json
- layout.json
The content that I put in there is based on the (awesome!) MobiFlight G1000/3000 Event ID module.
manifest.json:
{
"dependencies": [],
"content_type": "MISC",
"title": "WASM Event Module",
"manufacturer": "",
"creator": "Your name",
"package_version": "0.1.0",
"minimum_game_version": "1.11.6",
"release_notes": {
"neutral": {
"LastUpdate": "",
"OlderHistory": ""
}
}
}
layout.json
{
"content": [
{
"path": "modules/WASM_Module3.wasm",
"size": 11158,
"date": 132517602483582047
}
]
}
Note: “size” is the size of the WASM file in bytes. I have no clue about the “date” format, but it doesn’t seem to botter that much.
Then, in any other SimConnect code, call for instance:
hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_MFD_MODE_1, "#0x11000");
// ...
DOUBLE evdata;
// Set to evdata the required value 0 through 4.
SimConnect_TransmitClientEvent(hSimConnect, objectID, EVENT_MFD_MODE_1, evdata, SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
Hope this helps some people, and I am curious to learn more about how others tackle this issue.
Kind regards,
Maurice