FSUIPC: Lua Tutorial
For Microsoft Flight Simulator and GoFlight Equipment

 

Home
About Lua
Executing Code
Binary
Hexadecimal
Bits & Bytes
Memory Offsets
Variables
The Code
The Editor
Get Started
FlightSim
Cleaning Up
Assignments

 

Constants and Variables

    Computers use a lot of values.  Many of those values change a lot.  Two things we can use to keep track of values are called Constants and Variables.  In the Lua language, variables can be "called" anything. It can be made of letters and numbers.  They both can be used in mathematical formulas to produce a result. Variables are case-sensitive so x1 is not the same as X1.

            x = 1 + 2  or  x1 = 2 * 2  or  Me = 21 * 2

    In those examples, x = 3  and  x1 = 4  and  Me = 42

 

Constants

    A Constant is a variable that does not change.  In our Lua project with GoFlight, there are many Constants to help us keep track of our modules.  Each module has it's own number assignment but instead of trying to remember each one, we "label" them. For example, the GF-45 is number 1, GF-P8 is number 2, GF-T8 is number three ...the GF-TPM is number 16 and the GF-MESM is number 17. Those values won't change so, written into the FSUIPC interface with Lua are the constants GF45=1, GFP8=2, GFT8=3 ... GFTPM=16 & GFMESM=17.

 

Variables

    Now we're getting into the meat of programming.  If you look at any code, you will find lots of variables. They're used for a lot of things, mostly simple math.  A variable is simply a container, a place holder, a bit of short-term memory.  If we to do a lot of repetitive math, don't you write down the current value once in a while.  That piece of  paper just became a variable because the value on it can change.

    Variables are also classified as Local and Global

  • Local variables are only available to code within a chunk or routine. You must declare a variable as local or it will be considered a Global variable.
            Local Varname = x
  • There are two kinds of Global variables. One kind is available to the entire Lua file that's running.  The other is available to ALL running Lua files.
            Varname = x -- This variable is available to the entire Lua file
            ipc.set("Varname", x) -- This variable is available to ALL running Lua files.

    So where do we go from here...

 


Written by
Joseph "Skittles" Cardana
skittles(at)anadrac.com
Updated: 2011-08-25 08:00