Game Development Reference
In-Depth Information
Chapter 3
File Operations
When creating an app or a game, you need file I/O (input/output). You may not be working with
a database, but there will always be the need to save something back to the disk; it could be the
levels, the high score, a character the player might have unlocked, or some information regarding
in-app purchases.
So, let's first look at what we have available in terms of functions that offer file operation functionality.
Lua has two sets of functions for file operations, one called implicit and the other explicit . The
difference between the two is that implicit functions work on the default file as provided by the io
namespace, whereas explicit functions work with a file handle provided from a previous operation,
such as io.open .
Implicit Functions
This section lists and describes the implicit functions you'll be using in your Lua programming work,
accompanied by a short example of how each is used in code.
io.close ( [file] )
This function closes the output file; it is similar to file:close() , but when no file is passed, it
closes the default output file. You can see how io.close is used to close a fileHandle . To confirm,
we print the type of fileHandle to check if the file is open or closed.
fileHandle = io.open("file.txt", "w")
print(io.type(fileHandle))
fileHandle:write("Hello world")
io.close(fileHandle)
print(io.type(fileHandle))
29
 
Search WWH ::




Custom Search