Introduction to Lua Programming
Written By: S Kokila
- 31 Aug 2007 -
Description: Lua is a powerful, light-weight programming language designed for extending applications. Coupled with it being relatively fast and having a very lenient license, it has gained a following among game developers for providing a viable scripting interface. It has been used in games such as World of Warcraft and Far Cry, and in applications such as Adobe Photoshop Lightroom and Snort.
- What is Lua?
- Getting Started
- Identifiers, Types, and Values
- Variables and Expressions
- Operators
- Statements and Assignments
- Control Structures
- File I/O
Identifiers
Identifiers in Lua can be any string of letter, digits, and underscored, not beginning with a digit. This aligns with the identifier definition of most other languages. Some examples for valid identifier are sum, i, l21n, avg_marks, _mark, _MARK. However identifiers starting with an underscore followed by one or more uppercase letters should be avoided, as they are reserved for internal variables used in Lua.
The following are reserved and could not be used as identifiers in Lua:
and | break | do | else | elseif | |
end | false | for | function | if | |
in | local | nil | not | or | |
repeat | return | then | true | until | while |
Lua is case-sensitive, which means sum and SUM are two different identifiers.
Numerical constants may be written with an optional decimal part and an optional decimal exponent. Examples of valid numerical constants are 2, 3.0, 5.76, 0.576e1, 576E-2.
Types and Values
Lua is a dynamically typed language. The variables do not have a type associated with them, but the values do. The values carry their own types. The following types are the basic types in Lua:
nil | Represents the absence of a useful value. When a variable is not initialized, it is automatically set to nil in Lua. A value of nil makes a condition false. |
boolean | Represents the types of true and false values. A value of false makes a condition false. |
number | Represents the real, double precision floating point, numbers. |
string | Represents an array of characters. Lua is 8 bit clean and strings may contain any 8-bit character. Strings can be delimited by double or single quotes. |
function | Functions are first class values in Lua. That means, functions can be stored in a variable, passed as argument to other functions and returned from a function. |
userdata | Represents the block of raw memory. This type is provided to allow arbitrary C data to be stored in the Lua variables. |
thread | Represents the independent threads of execution, used to implement the coroutines. |
table | Represents the associative arrays that can be indexed with any value, except nil. That is, you can index a table with a number or a string or any value except nil. |
The type library function returns a string describing the type of a given value. The following chunk illustrates the type function:
print(type(a)) -- nil , as a is not assigned any value yet print(type(nil)) -- nil a=true print(type(a)) -- boolean a=10 print(type(a)) -- number a="hello" print(type(a)) -- string print(type(print)) -– function