The scope of this tutorial will be to record and store the highest score achieved by a player in a game. This is done by:
- Creating a file with a score of zero where the file does not exists.
- When a player dies:
- The previous score, i.e. value within the file, is assigned to a variable.
- The player’s new score is assigned to a variable.
- The two variables are compared.
- If the previous score is higher nothing is changed.
- If the previous score is lower the new score will be written to the file in its place.
No UI is provided as part of the tutorial as corona sdk often changes the manner in which objects are displayed, meaning the UI could break with future corona sdk versions. We will be working from the simulator output window alone.
For security reasons, you are not allowed to write files in the system.ResourceDirectory (the directory where the application is stored). You must specify either system.DocumentsDirectory, system.TemporaryDirectory, or system.CachesDirectory in the system.pathForFile() function when opening the file for writing. Read move about this here.
Below is a table describing when and where each directory should be used.
To use this tutorial create a folder containing a main.lua file.
Paste the code below into the file and save.
Open the file with Corona SDK and the score.txt file will be created and populated with a score of zero.
Play around with the Player Score variable:
newScore = 99
Enter a higher score and it will overwrite what currently exists in the file.
Enter a lower score and nothing will be changed.
local path = system.pathForFile( "score.txt", system.DocumentsDirectory )
deleteFile = function()
local result, reason = os.remove(path)
if result then
print( "File removed" )
print( "File does not exist", reason ) --> score.txt: No such file or directory
Uncomment below to remove file
-- Player Score
newScore = 99
-- io.open opens a file at path. returns nil if no file found
-- fh short for file handle
-- "r" is the read instruction
local fh, reason = io.open( path, "r" )
if fh then
-- Read all contents of file into a variable oldScore
-- This will be the previous score
-- To read file content as number use "*number"
-- To read file content as text use "*a"
-- "\n" new line
local oldScore = fh:read( "*number" )
print( "Old contents of " .. path .. "\n" .. oldScore )
if oldScore < newScore then
-- re-opening the file in "w+" mode will erase all previous data stored in the file
-- in the comments below is a table listing all the file mode types
fh = io.open( path, "w+" )
-- Set the score to the player's new score.
fh:write( newScore )
print( "New contents of " .. path .. "\n" .. newScore )
-- Error logic
print( "Reason open failed: " .. reason ) -- display failure message in terminal
-- create file because it doesn't exist yet
fh = io.open( path, "w" )
if fh then
print( "Created file" )
print( "Create file failed!" )
-- Set the score to zero.
fh:write( 0 )
io.close( fh )
The various file modes are listed in the following table:
"r" Read-only mode and is the default mode where an existing file is opened.
"w" Write enabled mode that overwites existing file or creates a new file.
"a" Append mode that opens an existing file or a creates a new file for appending.
"r+" Read and write mode for an existing file.
"w+" All existing data is removed if file exists or new file is created with read write permissions.
"a+" Append mode with read mode enabled that opens an existing file or creates a new file.
See the lua online book’s I/O library section for more information on working with files.