# Model Behaviors Component Overview To get a better idea of how model behaviors are constructed let's look at a simple component within the context of the `[model].xml` file. In this file, components are added within the [``](../../model-xml-properties/#CompileBehaviors) element, and in the example below you can see a component for changing the visibility of something: ``` xml STALL WARNING bool ``` In this example, you can see that the component is given an **ID** string and - in this case - a **Node**. {{< callout context="note" title="NOTE" icon="outline/bulb" >}} It should be noted that when using the visibility templates on a **skinned mesh** the Node ID that you use should be the *root node* of the part. {{< /callout >}} The node comes from the model {{< glossterm >}}gltf{{< /glossterm >}} and should be the name of a part in the 3D model. Within the component we tell the simulation what kind of behavior we are dealing with (visibility) and then define some parameters that will modify this behavior. In this case we're using a SimVar, which means that - in the simulation - the node for the model will be made visible/invisible depending on the value that the given SimVar has at any time. {{< callout context="note" title="NOTE" icon="outline/bulb" >}} In this example the `` and `` elements are required to define the SimVar, but there are also optional `` and `` elements for SimVars that can be included to. {{< /callout >}} This is the most basic way to add a component to the model XML and may even be the best approach when creating simple SimObjects other than aircraft. Note that the model behaviors XML is not just limited to using the [``](../general-template-xml-properties/#Component) element at it's top level, and it can also create [``](../general-template-xml-properties/#Macro) and [``](../general-template-xml-properties/#InputEvent) elements. You can find an explanation of these - and other - general template elements, on the following page: - [General Template Definitions](../general-template-xml-properties/)   The documentation also has an interactive **Template Explorer** which can be accessed here: - [Template Explorer](../templateexplorer/template-explorer/)   #### <ModelBehaviors> / <Behaviors> The `` element is how all new SimObject behaviors should be defined, but previously this was done using the `` and/or `` element, like this: ``` xml ```` xml ``` This format has been deprecated for the `[model].xml` files and should not be used for new projects. However, everything described in this section of the documentation can be applied equally to `` / `` as it can to ``. {{< callout context="note" title="NOTE" icon="outline/bulb" >}} `` is still a valid element when it is the root element for [Input Events](interactions/creating-cockpit-interactions/) and Event templates. {{< /callout >}}     ### Templates More complex SimObjects will require more complex behaviors, and in an attempt to streamline things for developers and make creating model behaviors easier, Microsoft Flight Simulator 2024 makes use of a **template** system. This is basically a large quantity of components that have been pre-created and stored in various template files, and each of these components can be referenced from within any other component, or directly from the `[model].xml` file in the [``](../../model-xml-properties/#CompileBehaviors) element. The idea here is to reduce the time and effort required to create complex systems, since all you need to do is add in a template, and then override only those values that are relevant to the model the behavior is for. {{< callout context="note" title="NOTE" icon="outline/bulb" >}} There is an interactive tool for exploring templates and model behaviors here: [Template Explorer](../templateexplorer/template-explorer/) {{< /callout >}}   Let's have a look at the visibility XML from before, but this time converted into a template: ``` xml ``` In the XML above you can see that we have created a template with the name "`ASOBO_GT_Visibility_Sim`", and then we define some default values for the template. The default parameters are there to set values for any *optional* parameters that the user want's to omit if they use the template. {{< callout context="note" title="NOTE" icon="outline/bulb" >}} The parameters of the XML will be processed in a **linear** order, from top to bottom. {{< /callout >}}   For more information on templates and how to use them, please see the following pages: - [Templates](templates/templates/) - [Default Templates](templates/default-templates/) - [Custom Templates](templates/custom-templates/) - [Important Templates](templates/important-templates/)     ### XML Programs One important feature of using the template system is that you can create "XML Programs" that use different XML elements to create small programs that will change the model behavior based on the outcome of some calculation or check. Generally you'll find these XML programs in the lowest level of the template hierarchy, in the "sub-templates", however they can be used just about anywhere.   To give an example of how these elements can be used, we'll build a "custom" visibility template example, and use sligtly more complex XML programming to introduce a *condition*. In this template we'll do a conditional check to use *either* a [SimVar](../../../../programming-apis/simvars/simulation-variables/) *or* some [RPN](../../../../programming-apis/reverse-polish-notation/) code to decide if the model node should be visible or not: ``` xml ```   This custom template is using a **conditional** check to see if the element `` is "empty" or not. {{< callout context="note" title="NOTE" icon="outline/bulb" >}} An "empty" node is a node that has been created but has no assigned value, while a node that does not exist would be considered as "undefined" rather than "empty". {{< /callout >}} If it is *not* empty - the condition is **True** - then the `ASOBO_GT_Visibility_Code` template will be used, and if it *is* empty - the condition is **false** - then `ASOBO_GT_Visibility_Sim` will be used. Where does the `` element come from? Well, that would be from some XML that is either in another template, or in the `[model].xml` itself, and it would look like this when using RPN: ``` xml NodeNameToHide (A:STALL WARNING, Bool) ```   Or it would look like this when using a SimVar: ``` xml NodeNameToHide STALL WARNING Bool ```   So, either one of the XML snippets above will call the template "`MyVisibilityTemplate`", and if you use the one with the `` element, then the conditional check will use the supplied RPN code to set the visibility, while if you use the other one, then a SimVar will be used. This is a *very* simple example of how a simple XML program is created, but there are a great number of program XML elements that can be used to modify the way any template resolves based on the given inputs.   You can find out more about all the specific programming elements from the following page: - [Program Template Definitions](../program-template-xml-properties/)   And you can find examples of the main "building block" elements for these programs on this page: - [XML Program Examples](../../model-xml-examples/#XMLPrograms)     ### Input Parameters When dealing with [Input Events](../input-event-xml-properties/), some initial value is normally passed in from the input source, which is usually a [``](../../animation-xml-properties/#MouseRect). This will contain some [RPN](../../../../programming-apis/reverse-polish-notation/) code that generates the initial value for the button, lever, switch, etc... that is being interacted with. This value is then passed through to the [``](../general-template-xml-properties/#InputEvent) as the variable **`pN`**, where *`N`* is a value starting at 0 and incrementing for each parameter that is being passed into the input event.   To give an example, we'll imagine a simple button that is used to set the COM volume to 50% . In this case our `` will have this snippet of RPN: 50 (>B:SOUND\_COM\_1\_Volume\_Set) This is calling the InputEvent "`SOUND_COM_1_Volume`", and is using the code defined in the [``](../input-event-xml-properties/#Set) element to generate the actual event. This element would look like this: ``` xml p0 0 max 100 min (>O:MyValue) (O:MyValue) (>K:COM1_VOLUME_SET) p0 ``` Here, all that's happening is that the value contained in `p0` is being passed into the input parameter without being modified, then it is being output and becomes the `p0` input used in the [``](../input-event-xml-properties/#Code) element, where the RPN uses it to set the "MyValue" component variable. This will then be used to set the `COM VOLUME` SimVar, as defined in the [``](../input-event-xml-properties/#Value) element of the XML: ``` xml percent (O:MyValue) (A:COM VOLUME:1, percent) (>O:MyValue) ``` {{< callout context="caution" title="IMPORTANT!" icon="outline/alert-triangle" >}} Even if you do not wish to modify the `p0` input, you **must declare it as a ``**, as shown in the `` example, above. Trying to access `p0` without first having assigned a parameter will cause errors. {{< /callout >}}   Another important thing to note is how the input parameters are affected by the use of RPN or not, since a [``](../input-event-xml-properties/#Param) that is *not* using RPN will not be considered as using an input parameter at all. For example, consider this XML that has various bindings with 2 parameters in each: ``` xml p0 if{ (>H:My_Event_True) } els{ (>H:My_Event_False) } p1 (>O:MyValue) p0 p1 1 p0 0 p0 (L:MY_VALUE_IS_ON) ! p0 ``` If you look at the alias "`EVENT_Set_Value_True`", here we only have one *input* parameter, which is `p0`, but we will be outputting two parameters: 1 *and* `p0` (in that order since the XML is parsed in a linear form). This means that in the "set" [``](../input-event-xml-properties/#Code), the first input parameter - `p0` - is 1 and the second input parameter - `p1` - is the initial input value from `p0`. To make it clearer, let's label the inputs and outputs: ``` xml 1 p0 ``` Essentially you can consider the `pN` format as simply defining a *local* scope variable to hold a value until the next operation.   You can find a full example that illustrates input parameters as well as the creation of a simple input event from the following section: - [Input Event Example](../../model-xml-examples/#InputEvents) You can also find more information on input bindings from the section below, and we have a full tutorial on using the vairous templates and parameters for simple interactions here: - [Creating Interactions Using InputEvents](interactions/creating-cockpit-interactions/)     ### Input Bindings An important feature of Input Events is that they can be *bound* to [Event IDs](../../../../programming-apis/key-events/key-events/) or even to each other. This means that you can use an Input Event to override a default key event behavior using your own code, or you can create aliases for different presets within your own behaviors, and even associate them to custom input actions, for example: ``` xml p0 16384 / 1 + 2 / p1 p0 (>L:XMLVAR_My_Throttle_Position) ```   With this binding any call made to the key `THROTTLE_SET` will be intercepted and with it all the original parameters associated with it. This is all explained in more depth and with examples on the following page: - [Model Behaviors Inputs](model-behaviors-inputs/)     ### Parameter Functions Much like [Templates](#Templates), parameter functions are a form of creating reusable XML that can be applied in multiple different circumstances without having to repeatedly type out the same elements. The concept here is that you can use [XML Programing elements](../program-template-xml-properties/) to create custom functions that can then be used within the different areas of the behaviors files. These functions are created using the [``](../general-template-xml-properties/#ParametersFn) element, which would be done within the [``](../../model-xml-properties/#CompileBehaviors) element. Once defined, the function can be called using the [``](../general-template-xml-properties/#UseParametersFn) element within a template or a component.   A definition consists of an *identifier* specified with the attribute `Name` and a *body* where we specify what our function does: ``` xml Hello world #GREET#, #USER#! ```   To make use of a function we use the [``](../general-template-xml-properties/#UseParametersFn) element, taking as an argument the name of the [``](../general-template-xml-properties/#ParametersFn). Any content defined within the [``](../general-template-xml-properties/#UseParametersFn) element will be parsed as parameters with the override rule. If we take the example above, let's look at how we can use it to return different values based on the additional elements that we supply: ``` xml Captain Hi Captain ```   A function can also be used to return a value depending on the context, or on a previously defined parameter: ``` xml I'm a switch I'm a button Switch ```   The goal of a parameter function is to help define parameters, since parameter definition can quickly become extremely verbose and it is often the of the function aim to do the same thing at several points in the code without repeating the same code. So [``](../general-template-xml-properties/#ParametersFn) are essentially templates but for parameter definition. Below you can see a more complex schematic showing how all this can come together: ``` xml #BASE_NAME# #BASE_NAME# A_NAME ```   In this example we have a function that will return two parameters (`NODE_ID` and `ANIM_NAME`) that have been set to hold the `` string. We then have a component that calls the function before using the parameters that it generates. This is the simplest and most straightforward way to use this functionality, but it's worth noting that within a parameter function you can use [XML Programing](../program-template-xml-properties/) to create more complex procedures for generating parameters, and you can also nest functions, eg: your function can perform some operation and then in the [``](../general-template-xml-properties/#ReturnParameters) call *another* function to perform further operations on the parameters. The snippet below illustrates this: ``` xml 0 1 2 On Auto Off #POS_ON_NAME# #POS_AUTO_NAME# #POS_OFF_NAME# ```   Finally, it should be noted that parameter functions can also make use of [Save parameters](model-behaviors-parameters/#save-params) to store information: ``` xml #INTERACTION_TYPE# Switch ```   You can find further examples for parameter functions on the following page: - [Parameter Function Example](../../model-xml-examples/#ParameterFunctions)