Tips and Tricks - MOARdV/AvionicsSystems GitHub Wiki

A collection of various tips & tricks (or best practices, if you prefer) collected during development.

Use Consistent Ordering

Try to be consistent with the order of values when using mathematical expressions in variable names.

MAS does not know that 2*fc.GetValue() and fc.GetValue()*2 have the same result. Thus, it will create a unique variable for each of these configurations, and it will evaluate the same thing twice, with all of the extra computational overhead that that entails.

Take Advantage of Boolean Mode when Possible

Many MASComponent actions support Boolean Mode, where the controlling variable is treated as true or false. Whenever possible, take advantage of that mode, since it requires the least computational overhead (all the action cares about is, "Is the new number greater than 0 or not?").

Use Lua in Variables Sparingly

Compared to C# code, Lua interpreted code is very slow - close to 16x slower than C# code. It is very flexible, so it should be used where its power is advantageous, but try not to duplicate existing functionality in Lua script. MAS has a built-in text parser, and it can convert most variables to simple lambda expressions. Lua is best suited for processing actions, where it is not called every FixedUpdate.

Use Text Markup Sparingly in Text that Only Changes Color

The various Formatted Rich Text markup tags (eg [b] and [/b], [@x#], etc) force Avionics System to generate the displayed text from scratch every time, even when only the color of the text is being changed. This is much more costly than simply changing the color for non-tagged text. Markup should be avoided for static labels that use color changing.

Config File Colors are Flexible

Most color parameters in config files may be configure in any of the following ways:

  • Static numbers, such as 255, 255, 255 (for RGB, with A=255) or 255, 255, 255, 128 (for RGBA).
  • Named Colors, which allow prop colors to be changed by editing one number, instead of hundreds of props.
  • HTML-style color tags, such as #ffff00 (for RGB, with A=255) or #ffffff7f (for RGBA).
  • Variable numbers, such as 127 + 128 * fc.GetThrottle(), 128, 128, which allow the color to change in response to another variable.

There are some exceptions to the above:

  • textColor in a MASMonitor TEXT node may never be a variable number.
  • (other exceptions will be added as I remember them)

If a particular color does not work as expected, please contact me - it may be a bug, or it may be an exception that I have not documented.

Automatic Scripted Action Can Be Created in Variables

If you want an automatically-triggered action to take place, it needs to reside in a variable used by a MASComponent or an active MASMonitor page. For instance, say you want an "automatic cutoff" function that will immediately turn off engines when a vessel lands. Use a persistent variable to control whether it's enabled (so it won't prevent launches, for example), and add a prop to your IVA that will be used to provide feedback whether the mode is enabled. In the prop, instead of querying the persistent, call a custom Lua function like this:

function myAutoCutoffFunction()
    local cutoffEnabled = fc.GetPersistentAsNumber("myCutoffEnabled")
    if cutoffEnabled > 0 then
        if fc.VesselLanded() > 0 and fc.GetEnginesEnabled() > 0 then
            fc.ToggleEnginesEnabled()
        end
    end

  return cutoffEnabled
end

This will return 1 if the cutoff is enabled, and 0 if it isn't. It also has the side-effect of making the cutoff trigger if it is enabled, the vessel is landed, and the engines are enabled.

Take Advantage of Inline Scripting

As long as you're not using Lua libraries or your own custom Lua scripts, MAS can parse a variable and convert it into a series of Lambda functions that can be evaluated natively in C# (meaning, much faster than Lua interprets them). All of the following variables are treated as "native" variables instead of Lua scripts:

Use the "Backlight" persistent as the variable:

variable = fc.GetPersistentAsNumber("Backlight")

Use 1 if landing gear is up, 2 if it is down:

variable = fc.GetGear() + 1

Set the variable to 25% of the backlight setting if landing gear is up, 100% of backlight is gear is down:

variable = (0.25 + 0.75 * fc.GetGear()) * fc.("Backlight")