Game Development Reference
In-Depth Information
animals = {
{"fly" , "But I don't know why she swallowed a fly, \nperhaps she will die"},
{"spider", "That wriggled and jiggled and tickled inside her"},
{"bird" , "quite absurd"},
{"cat" , "Fancy that"},
{"dog" , "what a hog"},
{"pig" , "Her mouth was so big"},
{"goat" , "she just opened her throat"},
{"cow" , "I don't know how"},
{"donkey", "It was rather wonky"},
{"horse" , "She's dead, of course!"},
}
line1 = "I knew an old lady who swallowed a %s,"
line2 = " She swallowed the %s to catch the %s,"
function printf(format, ...)
print(string.format(format, ...))
end
for i=1, #animals do
if i==1 then
printf(line1, animals[i][1])
else
printf(line2, animals[i][1], animals[i-1][1])
end
print(animals[i][2])
end
Parameter Handling
You can write functions to break longer and repetitive tasks making them modular. To make them
work for you, these functions need to be sent parameters. In some C-type languages, functions with
the same name but different parameter types can be declared. For example, you can have this:
function one(int, int)
and also this:
function one (int)
With Lua, the function can accept anything, including varargs, as mentioned earlier when we saw the
printf function. There are three types of parameters you can pass to the function:
A primitive : This can be a number , a string , a Boolean , or nil .
A table : This can be an array or a hash table.
A vararg : This is an arbitrary number of arguments, represented by three
dots ( ... ).
In the function, you can check for the type of parameter by using the type function, which will return
the type of variable the parameter is.
 
Search WWH ::




Custom Search