Game Development Reference
In-Depth Information
You can overwrite any function (including any Lua system function) by simply
assigning a new version of that function to the variable. For example, the following
code overwrites the print() function to add
[Lua]
in front of every print()
statement.
oldprint = print; -- save the old version of print
print = function(string)
local newString =
[Lua]
.. string
oldprint(newString)
end
This is a very important property of functions, as you will see later on in this chapter.
Save Old Functions
If you choose to overwrite existing Lua library functions, it
s a good practice to
save the old one in a global variable somewhere. If you don
'
'
t, you won
'
t have
any way to call upon the original behavior.
As I said before, functions can return multiple values. These values are separated
with a comma:
function MultiReturn()
return 2, 4
end
x, y = MultiReturn(); -- x will contain 2 and y will contain 4
Tables
Tables are Lua
s basic (and only) data structure. They are arrays and generic dictio-
naries all in one. Here
'
'
s a simple table:
prime = { 2, 3, 5, 7, 11 }
This statement declares a table with the first five prime numbers. A table is created
through the use of curly braces, which will actually instantiate a table object under
the covers. You can access the table using square brackets, just like in C.
print(prime[2]) -- > prints out 3
 
 
Search WWH ::




Custom Search