Difference between revisions of "Introduction to Lua"
Line 131: | Line 131: | ||
If A is false, then return B | If A is false, then return B | ||
|} | |} | ||
+ | |||
+ | === Arrays === | ||
+ | |||
+ | Arrays that can be indexed not only with numbers, but with any value. Arrays do not have to be defined a size, and they can grow as needed. When an array is defined, and you try to reach a value exceeding the boundary, you get a nil (instead of zero). | ||
+ | |||
+ | |||
+ | Arrays can have any index, e.g you can start with an array with -5, 0 or 1 - it’s all up to you. Consider the following example: | ||
+ | |||
+ | <source lang="lua"> | ||
+ | -- creates an array with indices from -2 to 2 | ||
+ | array = {} | ||
+ | for i=-2, 2 do | ||
+ | array[i] = "apples" | ||
+ | end | ||
+ | </source> | ||
+ | |||
+ | Lua also supports multi-dimensional arrays, which we call "matrices". | ||
+ | |||
+ | Consider the following example. A similar array syntax can be used to create string lists. The statement below: | ||
+ | |||
+ | <source lang="lua"> | ||
+ | animals = {"giraffe", "boar", "wolf", "frog"} | ||
+ | </source> | ||
+ | |||
+ | is equal to: | ||
+ | |||
+ | <source lang="lua"> | ||
+ | animals = {} | ||
+ | animals[1] = "giraffe"; animals[2] = "boar" | ||
+ | animals[3] = "wolf"; animals[4] = "frog" | ||
+ | </source> | ||
+ | |||
+ | === Linked Lists === | ||
+ | |||
+ | === Lua Tables (arrays) === | ||
+ | In Lua, Tables and Arrays are terms so used interchangeably, However this is true to an extent. | ||
+ | |||
+ | === Mathematical Functions === |
Revision as of 16:17, 10 September 2018
Overview
Lua was created in 1993 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, members of the Computer Graphics Technology Group at PUC-Rio, the Pontifical University of Rio de Janeiro. Gideros Studio benefits from Lua, a powerful, fast, lightweight, embeddable scripting language. Lua’s power comes from its simplicity and speed. Lua is mentioned as the fastest language among all interpreted scripting languages.
Lua is used in commercial applications, including Adobe's Photoshop Lightroom, World of Warcraft, Far Cry, Garry's Mod, Supreme Commander and Sonic the Hedgehog. It’s widely used in many applications, where speed and convenience is sought. You also write your applications in Lua inside Gideros Studio, however can also extend your apps with other languages like Objective-C, C++, C or Java.
Despite the size of the Lua interpreter (about 150Kb in size), Lua is a very rich, extensive and expandable programming language. Applications written using Lua looks very clean and understandable, yet effective.
Let’s start with printing a sentence, by writing down the following and running in Gideros Studio:
print ("Hello, Gideros Studio")
Here print() is a Lua function. We invoke a function using a paranthesis, and print takes the text between double quotes and writes to standard output.
We use "--" to start a comment, or use --[[ to start a mult-line comment. Consider this example:
--[[ This is a multi-line Lua comment
Where comment goes on and on...
And on...
]]
Let’s dig into Lua more.
Assignments and Variables
Lua is a dynamically-typed language, that is, variables don't have types, only the values. All values carry their own type. Consider the following as an example:
MyTable = wide
width = 3; height = 1.5
area = width * height
print (area)
You can nest several lines using semicolons, but it’s optional and generally not a good programming habit as it decreases the readability of your code.Be careful not to use reserved words. Gideros Studio will complain when you use a reserved word in Lua. Consider this example:
local = 1
Since Lua is a small language, it only has 8 basic data types:
- nil (null)
- Booleans
- Numbers
- Strings
- Functions
- Userdata
- Threads
- Tables
Global variables in Lua do not need to be declared. If you want to declare a variable, just assign a value to it. Giving it a nil value removes the variable. In fact, when a variable is removed, and it’s not referenced by another variable, it’s ready to be cleared up from memory, thanks to the wonderful Lua garbage collector.
Lua can support multiple assignments. Check the following assignment:
a, b, c = 1, 2, 3
-- Easy method to swap two variables
x, y = y, z
-- Function can return multiple values
n, m = calculate (phi, zeta)
Basic Functions
Lua has the ability to call its own functions or C functions, and it handles functions as a "data type". Note that functions can return multiple values, and you can use multiple assignments to collect these functions.
function calculate(w, d, h)
if h > 4 then
print ("height of the room cannot be more than 4”)
return
end
volume = w * d * h
return volume
end
-- Calculate the volume of the room
print (calculate (4,3,4))
Try the function above, this time with values 4,5,5.
String Manipulation
Lua has a powerful set of string manipulation functions, such as finding and extracting substrings. Unlike C, the first character of the string has "1" as the position index. You can also use negative indices in a string just like Python does - e.g the last character in a string is at position -1. You can use both methods, whichever you find convenient.
Note that the string library assumes one-byte character encodings.
The following table gives an overview of string manipulation commands. Have a look at them, and then study the examples below.
String Command | Meaning |
---|---|
string.byte (s [, i [, j]]) | Returns the internal numerical codes of the characters s[i], s[i+1], ···, s[j]. The default value for i is 1; the default value for j is i. |
string.find (s, pattern [, init [, plain]]) | Looks for the first match of pattern in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil |
string.format (formatstring, ···) | Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string). |
string.gmatch (s, pattern) | Returns an iterator function that, each time it is called, returns the next captures from pattern over string s. |
string.len (s) | Returns the length of the string s. |
string.lower (s) | Changes all characters of a string to lowercase. |
string.upper (s) | Changes all characters of a string to uppercase. |
string.reverse (s) | Returns a string which is a reverse of the string s. |
Control Structures
Lua provides a strong control mechanism between different variable types. As an example for logical decisions, consider the following:
Logical Decision | Meaning |
---|---|
A and B | If A is true, then return the result of expression B.
If A is false, then return A |
A or B | If A is true, then return the result of expression A
If A is false, then return B |
Arrays
Arrays that can be indexed not only with numbers, but with any value. Arrays do not have to be defined a size, and they can grow as needed. When an array is defined, and you try to reach a value exceeding the boundary, you get a nil (instead of zero).
Arrays can have any index, e.g you can start with an array with -5, 0 or 1 - it’s all up to you. Consider the following example:
-- creates an array with indices from -2 to 2
array = {}
for i=-2, 2 do
array[i] = "apples"
end
Lua also supports multi-dimensional arrays, which we call "matrices".
Consider the following example. A similar array syntax can be used to create string lists. The statement below:
animals = {"giraffe", "boar", "wolf", "frog"}
is equal to:
animals = {}
animals[1] = "giraffe"; animals[2] = "boar"
animals[3] = "wolf"; animals[4] = "frog"
Linked Lists
Lua Tables (arrays)
In Lua, Tables and Arrays are terms so used interchangeably, However this is true to an extent.