Lua Syntax
From Data Realms Wiki
Syntax If you have no idea how to use Lua, start here.
| Datatypes Descriptions of the data types Lua uses.
| ||||
Functions Listing of known functions.
| 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.
Contents |
Variables
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:
- x = 21 --Integer
- y = true --Boolean
- z = "Cortex" --String
Comments
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: "--"
- --This is a proper comment.
- function() --Comments can also be placed after a statement.
- --[[Or this format can be used
- to make a block comment,
- which can span multiple lines.
- ]]--
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.
Math
The math operators are common math signs:
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.
Symbol | Purpose |
---|---|
+= | Addition |
-= | Subtraction |
*= | Multiplication |
/= | Division |
Example:
- x = 1 --x is 1
- x = x + 1 --x is 2
- x += 1 --shorthand form; x is 3
Equality
These operators are usually used with control structures and return a boolean value.
Symbol | Purpose |
---|---|
== | is equal to |
~= | is not equal to |
>= | is greater or equal |
<= | is less or equal |
> | is greater than |
< | is less than |
Example:
- x = 5 --assignment
- x == 5 --true
- x ~= 5 --false
Control Structures
If/then
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.
Example:
- x = 1
- if x == 1 then y = 1 end
or
- x = true
- if x then y = true end
For
A for loop is, simply put, a block of code that's executed multiple times.
Example:
- x = 0
- for i = 1, 10 do
- x = x + 1
- end
While
A while loop runs indefinitely, or while a condition is met.
Example:
- x = 1
- while x < 5 do -- "while x is lesser than five, do"
- x = x + 1 -- once x is greater than five, the while loop will cease to execute until x is lesser than five again
- end
Functions
A Lua function is a set of instructions invoked as a group by name with the syntax:
- 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:
- object_name:function_name(arguments)