Lua data sharing across scripts

Overview:

OpenTX considers all function, mix, and telemetry scripts to be 'permanent' scripts that share the same runtime environment. They are typically loaded at power up or when a new model is selected. However, they are also reinitialized when a script is added or removed during model editing.

Lua scoping rules:

Any variable or function not declared local is implicitly global. Care must be taken to avoid unintentional global declarations, and ensure that the globals you intentionally declare have unique names to avoid conflicts with scripts written by others.

Example:

This example consists of three scripts

  • count-dn.lua - this is a mix script than can be run stand alone to announce time remaining based on a user-defined switch and duration. It updates two global variables (gCountUp and gCountDown). It also creates output values (ctup and ctdn) which are for demonstration purposes only.

  • count-up.lua - this is an optional function script which will do count up announcements based on harded coded values.

  • shocount.lua - this is an optional telemetry script which simply shows the current values of the gCountUp and gCountDown variables.

Installation:

  • count-dn.lua

    • copy to /SCRIPTS/MIXES

    • configure on the transmitter CUSTOM SCRIPT page

      • suggested switch = "SA"

      • suggested mins = 3

      • suggested sw_high = 0

    • screen image:

      count-dn.lua mix script

  • count-up.lua

    • copy to /SCRIPTS/FUNCTIONS

    • configure on the transmitter SPECIAL FUNCTIONS page

      • suggested switch SA(down)

    • screen image:

      count-up.lua function script

  • shocount.lua

    • copy to /SCRIPTS/TELEMETRY

    • configure on the transmitter TELEMETRY page

    • screen image:

      shocount.lua function script

Script sources:

count-dn.lua

count-up.lua

shocount.lua

Last updated

Was this helpful?