Game Development Reference
In-Depth Information
The math namespace provides all the math-related functions; these are the basis for all the logic in
most games. The math namespace provides all math-related functions that can help calculate things
like where the player is, where the objects are, whether the player won or lost, and much more. We
shall look at this namespace in detail in Chapter 4.
The file namespace, covered in Chapter 3, provides file-related functions, including those that
allow the user to read, write, and delete files. Note that file operations do not include much file
system-related functionality, and there is a third-party library called Lua File System (LFS) that offers
the missing functionality.
The os namespace provides functions that are related to OS-specific functions, including functions
that deal with things like time, date, and locale. We shall look into this further ahead in the “OS
Functions” section of this chapter.
The way to use these libraries is to prefix the library name followed by a dot and the function. This
is a good way of separating the functionality available in the library from the common or global
area. With libraries, variables and functions can be part of the namespace. For example, if we had a
function a1 , and the namespace myFunc had a function defined as a1 , these two would be completely
different functions. To access these functions, simply add the namespace as a prefix in front of the
function name, as follows:
-- call the global a1 function
a1()
-- calling the a1 function available in the namespace myFunc
myFunc.a1()
There is no function called a1 , so these commands will not work; these are just for illustration purposes.
Note
Table Functions
You may have already noticed that in Lua, if anything is not a string or a number, then it is an object
(i.e., a table). Tables are the most important data type in Lua. There are a handful of functions to
manipulate end work with the tables. These are useful functions for working with array-type tables.
The functions available are described in the following subsections.
table.concat ( aTable [,sep [,i [,j] ] ] )
This function returns the elements of an array (if they are either strings or numbers) concatenated as
a string. These are separated by a separator, as indicated by sep , which is a blank string by default.
If no parameters are passed for i and j , then i is the first element and j is the length of the table.
If i is larger than j , then an empty string is returned.
If you were to write this function yourself, it would look like
return aTable[i]..sep..aTable[i+ 1] ... sep..aTable[j] .
local aryTable = {1, "deux", 3, "vier", 5}
print( table.concat( aryTable, ",", 1,3))
print(table.concat(aryTable))
 
 
Search WWH ::




Custom Search