Introduction to Lua
Lua
Creator: Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, Pontifical Catholic University of Rio de Janeiro, Brazil
Introduced: 1993
Open source
Installable package on many UNIX/Linux systems
-
Can be used for tasks that other dynamic programming languages are used for.
Has gained its most prominent footholds in the areas of embedded programming because of its relatively small size and as a scripting language for large games
UI modification in World of Warcraft, Rift, Age of Conan, Lord of the Rings Online, Warhammer Online, Runes of Magic and others
-
-
Several Lua-based game development platforms, including commercially-licensed
-
Lua Online Resources
Lua Execution
Programs typically given .lua extension
Can be executed with lua prog.lua
Interactive shell by running lua
-
Lua Syntax
syntax very similar to Python and even more similar to Ruby
strength is in processing strings and tables
uses
coercion for every integer and number type to convert it into a single type
Tables (like dictionaries or hashes) are the only data structuring mechanism that Lua has.
Object-oriented programming implementation is minimalistic.
Data Types
Variables and Identifiers
Because any value can represent a condition, booleans in Lua differ from those in many other languages.
Both false and nil are considered false in Lua, but Lua considers everything else true (including zero and an empty string).
Global and local variables
Unlike Python, global variables do not need to be declared.
To create one, assign a value to it. To delete it, give it the nil value.
Most variables in Lua are global by default, and you must declare the variable “local” to make it a local variable.
No integer types
Because most CPUs perform floating-point arithmetic just as fast as integer arithmetic, numbers in Lua represent real, double-precision, and floating-point numbers rather than common integers.
Lua doesn't need integer types, so it doesn't have them.
Rename anything
x = io
x.read()
io = "Hello world!"
x = "Let's make io uncallable!"
io.read()
The second line–x.read()–gets keyboard input through the io module.
Because io is essentially a variable with a function as a value, you can give it a different value so that io does not relate to the input/output functions anymore. (line 3)
In line 5, when you try to get keyboard input from the io module again, Lua returns an error.
The program is unable to call the input/output functions now that io's value has been reassigned.
In order to use io again, you must restart the Lua program.
Operators and Assignment
Concatenate strings with ..
print(“Hello”..“World!”) is valid.
print(“I've said 'Hello World'”..5..“or more times.”) is not valid.
print(“I've said 'Hello World' ” ..5 .. “ or more times.”)is valid.
Logical not operators
Strings and integers
Lua treats strings and integers differently.
Although print(“1” * 2) prints 2, “1” < 2 is always false.
Lua and OOP
Lua supports OOP, but due to Lua's size, its implementation of OOP lacks a few features.
Lua uses tables and functions for OOP rather than classes.
In the same way that Python accesses a member function or member variable of a class, Lua accesses it with Table.function or Table.variable.
Because Lua does not have the class concept, each object defines its own behavior and shape:
-- This is a Lua comment.
-- Definition of the Earth "class"
-- starts with a table called Earth:
Earth = {martians = 5389}
function Earth:casualties (survivors)
Earth.martians = Earth.martians - survivors
print("Earth is free! "..Earth.martians.." martians survived!")
end
-- Call the Earth:casualties method:
Earth:casualties(5380)
-- The casualties function is part of the table this time
-- so that it can be called via the dot or the colon syntax.
Earth = {martians = 5389,
casualties = function (self, survivors)
self.martians = self.martians - survivors
print("Earth is free! "..self.martians.." martians survived!")
end
}
Earth.casualties(Earth, 5380)
Earth.martians = 5389
Earth:casualties(5380)
Lua vs. Python
print("What's your name?")
name = io.read()
questions = {
["Which came first? Minix or Unix?"] = "Unix",
["Who created Linux?"] = "Linus Torvalds",
["In what year was Linux created?"] = "1991"
}
correct_answers = 0
for key,value in pairs(questions) do
print(key)
answer = io.read()
if answer == value then
correct_answers = correct_answers + 1
end
end
if correct_answers == 0 then
print("You need to browse Wikipedia!")
else
print("\nGood job, "..name.."!")
print("Correct answers: "..correct_answers..")
end
* Equivalent Python code
name = raw_input("What's your name?\n")
questions = {"Which came first? Minix or Unix?":"Unix",
"Who created Linux?":"Linus Torvalds",
"In what year was Linux created?":"1991"}
correct_answers = 0
for key in questions:
print key
answer = raw_input()
if answer == questions[key]:
correct_answers += 1
if correct_answers == 0:
print "You need to browse Wikipedia!"
else:
print "\nGood job, " + name + "!"
print "Correct answers: ", correct_answers
Lua Tables
See the Tables Tutorial at lua-users.org.
For an example of a two-dimensional array implemented through tables, see the LOVE game framework Gridlocked Player example.
Patterns (Regular Expressions)
Coroutines (Threading)