Skip to content

MSFS2020 User Defined WASM Module Events Best Practices

Jaime Leon edited this page May 14, 2024 · 24 revisions

Prepared by Jaime Leon - 8-Jun-21 _Updated by Jaime Leon - 14-May-24

Version 0.30

I have decided to update and rewrite this document in view that three years have passed and both MSFS2020 and Mobiflight have evolved.

GENERAL DOS AND DON’TS

  1. Do learn RPN scripting. You will need to learn a little about this topic to understand the scripts used in MSFS. The scripting language used by MSFS is called RPN (“Reverse Polish Notation”). It is the way old HP engineering calculators worked, where numbers (or variables) are entered first and then the operand to be executed is entered last. The evaluation of RPN scripts is done using a stack operation analogy. For more information, see the RPN scripting guide in the MSFS SDK down below. Just to get a feel, the following example reads a variable value in feet and adds 100.

    (A:PLANE ALTITUDE, feet) 100 +

*** EVENTS.TXT USE IS DEPRECATED *** All code can be input directly to the newer Input and Output code boxes in Mobiflight.

  1. Variables and in-sim events (not wasm module events) names are enclosed in parenthesis. No space after the opening, nor space before the closing parenthesis. A variable will have a type (like A:, L:, H:, I:, M:, O:, etc), followed by the variable name, followed by an optional index (:1), followed by a comma, and followed by the optional units (percent, percent over 100, second, feet, mhz, khz, number, enum, bool, etc.). As an example:

    (A:ENGINE ANTI ICE:1, Bool)

    is an A type variable (or “Avar”, short for Airplane variable), whose name is ENGINE ANTI ICE, it is being accessed with an index of 1, and the variable is a Boolean (only takes 0 and 1 values). The space after the comma is optional.

  2. Do note that event and script names are not case sensitive. The event names may contain any combination of letters and numbers, including spaces and underscores. The letters in the name are not case sensitive, but the variable types are required to be in upper case. So, for example:

    0 (>L:MYVARIABLE_NAME)

    is the same name and code as the following:

    0 (>L:myVaRiaBle_Name)

    Note the upper case “L” in the variable type is required. The rest is not case sensitive. Although not strictly necessary, we follow the case use (upper or lower) in the same way a variable is presented in MSFS. For example:

    (L:XMLVAR_Pitot_1, bool)

    will normally be shown the same way in our documented scripts. Also, the units are not case sensitive: bool, Bool, and BOOL are all equivalent.

  3. Do note the meaning of the > sign after the opening parenthesis. This means to write a value to this variable or in the case of events, execute this event. Be careful when transcribing code, as it is very easy to miss entering the > and later it is very hard to spot the error. Examples follow:

    (L:my_var) ++ (>L:my_var) read my_var, increase value by 1, store in same variable

    1 (>L:ASCRJ_FCP_APPR) set the variable to a value of 1

    (>K:AP_AIRSPEED_ON) execute the autopilot air speed on event

    1000 (>K:AP_ALT_VAR_INC) use parameter 1000 to execute autopilot altitude increase

  4. Do use unique names. USE OF EVENTS.TXT IS DEPRECATED. The Hubhop events database has a unique GUID for each event, so event name labels are just as a reference and althought it is highly desirable to have unique names, it is not a technical requirement any more. To prevent unintended duplicate names, it is suggested to prefix all your events with the target airplane, for example

    CJ4_AUTOPILOT_MASTER_ON or TBM930_PULSE_SWITCH_ON

  5. Do use names that can be understood by others. The computer doesn’t care, but we do. Using "A32NX_AP_MASTER_ON" is much better than "A32NX_B2355", although both are valid names. Use shorter names and abbreviations when reasonable, without becoming too cryptic. Don’t include unnecessary words in your names. Nobody likes very long event names. Assume other people will need to understand and use your event.

  6. Do pay close attention to the spaces in the code. This is important. The code parser is very unforgiving. There is no space after the if and els clauses. There is no space after the opening and before the closing parenthesis in a variable name. There MUST be at least one space between two variables or between any other elements in a script.

  7. New InputEvents tab in Model Behaviors. Since MSFS2020 version 1.18.13, a redesign of the Model Behaviors window was made. Among the new features, there is a tab called "InputEvents" that shows all the registered events for a particular airplane, with the code related to each event. When selecting one event in this page, there is an option to "Add to control panel" which opens a new box to the right with the event details. Variations of the same event are presented in tabs: Init, Value, Inc, Dec and Set. The code presented in these boxes can be selected with the mouse and copy/pasted to a code editor. This makes it far easier to extract, edit and test events. Many more functions are available from this tab, but are beyond the scope of this writing.

CONCLUSION

This search, testing and recording of simulator events is an ongoing effort of the Mobiflight community, so that other members can benefit from our previous accumulated experiences and be able to build panels or home cockpits for flight simulation more easily.

If you have questions or comments, be sure to join us in the Mobiflight forum and the Mobiflight Discord server, where you will be able to find help from more experienced members and the latest news about this excellent piece of software.

GLOSSARY OF TERMS

Boolean, bool A type of variable that represents one bit of information and therefore only takes values of 1 or 0. Also interpreted as True or False, or as ON or OFF. Extensively used to represent the state of switches, LEDs, or even entire systems. It is the building block of binary logic.

LVAR “Local Variables” created by the mod airplane designers, often used to provide support for functions not included in the simulator

RPN “Reverse Polish Notation” Scripting language used in MSFS to program events, characterized by its reverse order notation, where numbers are entered first and the operands last

WASM “WebAssembly” is a new type of code that can be run in modern web browsers — it is a low-level assembly-like language with a compact binary format that runs with near-native performance

ONLINE RESOURCES REFERENCE

MSFS SDK Documentation: https://docs.flightsimulator.com/html/index.htm#t=Introduction%2FIntroduction.htm&rhsearch=status&ux=search

RPN Scripting: https://docs.flightsimulator.com/html/Additional_Information/Reverse_Polish_Notation.htm

https://www.prepar3d.com/SDKv5/sdk/scripting/rpn_scripting.html

Hubhop online events and variables presets database: https://hubhop.mobiflight.com/#/

Mobiflight web page: https://www.mobiflight.com/en/index.html

Mobiflight Github: https://github.com/Mobiflight/MobiFlight-Connector

Mobiflight Discord server: https://discord.gg/99vHbK7

Youtube Video: https://www.youtube.com/watch?v=PKBjEl9E5A4

Installation

User Interface

Examples and tutorials

MSFS2020

X-Plane

Workshops

Community Boards & Devices

Additional Information

Firmware

Clone this wiki locally