Data saving

Saving file information is used in many aspects of game development. We use it to save high scores and game settings, such as sound on/off, locking/unlocking levels, and so on. It is not necessary to have these features, but as they are good to have, maybe you'd like to include them in your applications.

In Corona SDK, applications are sandboxed; this means that your files (application images, data, and preferences) are stored in a location that no other application can access. Your files will reside in an app-specific directory for documents, resources, or temporary files. This restriction is related to the files on your device, not when you are coding on your Mac or PC.

We'll be using the BeebeGames class created by Jonathan Beebe. It provides many easy and useful functions to use for games. Some of the notable functions included incorporate a simple way of saving and loading data that we'll be able add into our game. More documentation on the BeebeGames class can be found in the Chapter 8 folder.

You can take a look at other methods relating to animation, transitions, timers, and so on if you would like to use them for future use. For now, we'll focus on the methods we can use to easily save and load values for our game.

Here is an example of saving and loading values:

-- Public Method: saveValue() --> save single-line file (replace contents)

function saveValue( strFilename, strValue )
  -- will save specified value to specified file
  local theFile = strFilename
  local theValue = strValue

  local path = system.pathForFile( theFile, system.DocumentsDirectory)

  -- io.open opens a file at path. returns nil if no file found
  -- "w+": update mode, all previous data is erased
  local file = io.open( path, "w+" )
  if file then
  -- write game score to the text file
  file:write( theValue )
  io.close( file )
  end
end


-- Public Method: loadValue() --> load single-line file and store it into variable

function loadValue( strFilename )
  -- will load specified file, or create new file if it doesn't exist

  local theFile = strFilename

  local path = system.pathForFile( theFile, system.DocumentsDirectory)

  -- io.open opens a file at path. returns nil if no file found
  -- "r": read mode
  local file = io.open( path, "r" )
  if file then
    -- read all contents of file into a string
    -- "*a": reads the whole file, starting at the current position
    local contents = file:read( "*a" )
    io.close( file )
    return contents
  else
    -- create file b/c it doesn't exist yet
    -- "w": write mode
    file = io.open( path, "w" )
    file:write( "0" )
    io.close( file )
    return "0"
  end
end

The paths to these files are unique to your application. To create file paths, you can use the system.pathForFile function. This function generates an absolute path to the icon file for your application, using the application's resource directory as the base directory for Icon.png:

In general, your files must reside in one of the three possible base directories:

To read files, the io library is used. This library allows you to manage files, given an absolute path.

To write files, you follow many of the steps that are the same as for reading a file. Instead of using a read method, you write data (strings or numbers) to a file.