Time for action – saving and loading the high score

When the Game Over screen displays, we will save and load the values of our final score and highest score. For this perform the following steps:

  1. Open up your main.lua file that we created for Egg Drop. We'll continue using the same file and add in more code with the new alterations to the game.
  2. Add in two new variables, local highScoreText and local highScore where all the other initialized variables are located, near the top of the code:
    local highScoreText
    local highScore
  3. Introduce the saveValue() function after the preloaded sound files:
      local saveValue = function( 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
        local file = io.open( path, "w+" )
        if file then
          -- write game score to the text file
          file:write( theValue )
          io.close( file )
        end
      end
  4. Add in the loadValue() function:
      local loadValue = function( 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
        local file = io.open( path, "r" )
        if file then
          -- read all contents of file into a string
          local contents = file:read( "*a" )
          io.close( file )
          return contents
         else
          -- create file b/c it doesn't exist yet
          file = io.open( path, "w" )
          file:write( "0" )
          io.close( file )
           return "0"
        end
      end
  5. At the end of the callGameOver() function, create an if statement to compare gameScore and highScore. Save the highest score by using the saveValue() function:
        if gameScore > highScore then
          highScore = gameScore
          local highScoreFilename = "highScore.data"
          saveValue( highScoreFilename, tostring(highScore) )
        end
  6. Next, add in the highScoreText display text in the same callGameOver() function, to show the high score at the end of the game:
        highScoreText = display.newText( "Best Game Score: " .. tostring( highScore ), 0, 0, "Arial", 30 )
        highScoreText:setTextColor( 1, 1, 1 )	
        highScoreText.xScale = 0.5; highScoreText.yScale = 0.5
        highScoreText.x = 240
        highScoreText.y = 120
    
        gameGroup:insert( highScoreText )
  7. At the end of the gameStart() function, have the high score loaded by using the loadValue() function:
          local highScoreFilename = "highScore.data"
          local loadedHighScore = loadValue( highScoreFilename )
    
          highScore = tonumber(loadedHighScore)
    Time for action – saving and loading the high score

After initializing the saveValue() and loadValue() functions at the game level, we created an if statement to compare gameScore, which is the current score during game play, and highScore, which is the highest score accrued so far. When the outcome of gameScore is higher, then it replaces the highScore data saved.

In order to save the value, a data file needs to be created. We created a variable called local highScoreFilename = "highscore.data". We called the saveValue() function using highScoreFilename as a parameter. The tostring(highScore) parameter will convert the numeric value of highScore to a string.

When the Game Over screen is visible, highScoreText displays the value saved from highScore above the gameScore that is achieved. Adding a high score gives the player an incentive to top the highest score and adds the replay value to the game.

In the gameStart() function, it's important to have the value of highScore.data loaded at the start of game play. Using the same data file we created to save highScore, we can also load the value throughout the game. To load the value, local highScore calls loadValue(highScoreFileName). This takes the information from highScore.data. To obtain the value, tonumber(loadedHighScore) converts it to an integer from a string and can be used to display the value of highScore.