Lua Syntax

From Data Realms Wiki

Jump to: navigation, search
Lua Syntax Syntax
If you have no idea how to use Lua, start here.

Lua Datatypes Datatypes
Descriptions of the data types Lua uses.

List of all Lua functions Functions
Listing of known functions.

LuaDocs/Index Official Reference
Raw output generated directly from Data's source.

Here's a little crash-course in Lua syntax, as it pertains to Cortex Command.



A variable stores a value. The value can be any of a few datatypes. Here are examples to assign variables x, y, and z number, boolean, and string values respectively:

  1. x = 21 --Integer
  2. y = true --Boolean
  3. z = "Cortex" --String


In programming, comments are parts of code that are completely ignored by the reading program. In Lua, this is accomplished with the use of two hyphens right next to each other: "--"

  1. --This is a proper comment.
  2. function() --Comments can also be placed after a statement.
  4. --[[Or this format can be used
  5. to make a block comment,
  6. which can span multiple lines.
  7. ]]--

Variable Manipulation

Variables of any datatype can be manipulated in a variety of ways. We will explain the methods of computing, comparing, and assigning variable values.


The math operators are common math signs:

Math Operators
Symbol Purpose
+ Addition
- Subtraction
* Multiplication
/ Division

Assignment & Math

These operators are shorthand; they first add the inputs, and then assign the result to the first.

Math Operators
Symbol Purpose
+= Addition
-= Subtraction
*= Multiplication
/= Division


  1. x = 1 --x is 1
  2. x = x + 1 --x is 2
  3. x += 1 --shorthand form; x is 3


These operators are usually used with control structures and return a boolean value.

Comparison Operators
Symbol Purpose
== is equal to
~= is not equal to
>= is greater or equal
<= is less or equal
> is greater than
< is less than


  1. x = 5 --assignment
  2. x == 5 --true
  3. x ~= 5 --false

Control Structures


One of the simplest logical constructions, the first statement is evaluated as a boolean. Equality operators are useful to this effect. If the first statement is true the statement after then is executed.


  1. x = 1
  2. if x == 1 then y = 1 end


  1. x = true
  2. if x then y = true end


A for loop is, simply put, a block of code that's executed multiple times.


  1. x = 0
  2. for i = 1, 10 do
  3. x = x + 1
  4. end


A while loop runs indefinitely, or while a condition is met.


  1. x = 1
  2. while x < 5 do -- "while x is lesser than five, do"
  3. x = x + 1 -- once x is greater than five, the while loop will cease to execute until x is lesser than five again
  4. end


A Lua function is a set of instructions invoked as a group by name with the syntax:

  1. function_name(arguments)

In Lua, the trailing parenthesis are not necessary for control structures such as if or for, but may be useful to maintain logical flow while reading code. If in doubt, parenthesize. Another important facet of functions is their return value. A function can be used in the stead of a datatype as long as its return value is of that datatype.

Member Functions

You may have wondered at the term "member function," but this is simply a function that is organized into a group of functions and variables which in Cortex Command are the Manager objects. Proper syntax for accessing member functions in Lua:

  1. object_name:function_name(arguments)
Personal tools