SimConnect + WASM combined project using VS2019

Chapter 5: Variable housekeeping using object VarData

This chapter is a bit “off topic”, but to avoid that people get completely lost in my implementation, I will take some time to explain.

In my object SimConnectHUB, variables are maintained in a “List” with objects of type “VarData”.

// List of registered variables
private List<VarData> Vars = new List<VarData>();

The details of object VarData can be found in the file “VarData.cs”. This object takes care of a few things:

  • Storage of all kinds of values that are needed for the variable (ID’s, name, type, etc…)
  • A helper method “SetID” to assign ID’s, and eventually create them automatically
  • A set of methods to implement the “IEquatable” interface, which allows comparing 2 VarData objects (needed to check if a VarData item already exists in the List, to avoid duplicates)
  • A method “ParseVarString” that parses a string provided by the user, extracting all the values and report if that was successful

The VarData method public ParseResult ParseVarString(string sVar) takes a string with a specific format, which depends on the variable type. The method validates the string sVar, and extracts all the neccessary values. If successful, it will return the enum “ParseResult.Ok”. If not, it will return an error that gives a hint on what is wrong.

There are currently 3 variable types supported:

  1. Type "A"
    These are “native MSFS2020 variables”
    Format: “A:[Variable name],[unit],[DataType]”
    Example: “A:AUTOPILOT ALTITUDE LOCK VAR:3,feet,FLOAT64”

  2. Type "L"
    These are “custom variables”
    Format: "L:[Variable name],[unit]
    Example: “L:A32NX_EFIS_L_OPTION,enum”

  3. Type "K"
    These are events.
    Format: "K:[Event name] (if it contains a “.”, it is a custom event)
    Example of native event: “K:FUELSYSTEM_PUMP_TOGGLE”
    Example of custom event: “K:A32NX.FCU_HDG_INC”

The object SimConnectHUB implements 3 methods that make use of the VarData object and the List.

  • public bool AddVariable(string sVar) - called when pressing the button “Add Variable”
  • public bool RemoveVariable(string sVar) - called when pressing the button “Remove Variable”
  • public bool SetVariable(string sVar, string sValue) - called when pressing the button “Set Value”

Each method will start with doing some “housekeeping” like checking if we are connected, checking if the user input is valid (not empty and parsing is successful) and checking if the variable does or does not exist already. If it passes all the tests, the real SimConnect work can start which will be explained in the next chapters.

I will not spend more time in explaining the implementation of VarData, because this tutorial is about SimConnect. But I think that the implementation is pretty self-explanatory. If you would have any questions, don’t hesitate to ask of course.

Goto Chapter 6: Controlling the “A:”-type variables

2 Likes