Corona SDK: Save Level Progress

In my previous post I showed you how you can make a level selection template in Corona SDK. In that example you could select different levels from a gridview and each level had a state: open, locked or completed. In this post I extend the template to also save the progress of the player. That way, when the user returns back to the game the states of each level are retrieved from the local storage.

This post is hard to follow without having read the previous post.

Create another LUA file called levelstorage.lua.

In this lua file we will create two functions. First, to load the levels and a second function to save the level states. Because we are using a table to represent the different level states (open, locked, and completed) we need to convert the contents of the table into a JSON. Because standard the write, read and append to local storage within Corona only supports strings.

In my previous post, level template, we store each state of a level into an array

levels = 
{	
1, 2, 2, 2 , 2,   --1 means level is open to 	be played (level.png)
2, 2, 2, 2, 2,   --2 means level is locked (locked.png)
2, 2, 2, 2, 2  -- 3 means level is completed (greenchecked.png)
}

JSON is supported by the Corona SDK framework, so you need to import the JSON library.

The complete levelstorage.lua file should look like this:

local json = require("json")


function loadLevels()
		local base = system.pathForFile( "levels.json", system.DocumentsDirectory)
	 	local jsoncontents = ""
	 	local levelsArray = {}
	 	local file = io.open( base, "r" )
	 		if file then
	 			local jsoncontents = file:read( "*a" )
	 			levelsArray = json.decode(jsoncontents);
	 			io.close( file )
	 	        return levelsArray	
     		end
    		
     		return levels
	 end

function saveLevels()
	    local base = system.pathForFile( "levels.json", system.DocumentsDirectory)
	    local file = io.open(base, "w")
	    local jsoncontents = json.encode(levels)
	    file:write( jsoncontents )
		io.close( file )
	end

The saveLevels() function creates a json file called levels.json in the system.DocumentsDirectory on the device and opens the file for writing data (“w”). The levels table is then encoded into json and written to the json file we just created. When you call this function for the second time no json file is created by the “w” (write) but will overwrite the current json table.

To load the levels we use loadLevels(). In this function we “r” (read) the json file if the file does not exist we read the levels table. Which is the initial set-up of level states as defined in play.lua. (see my previous post). In case an existing json file does exist (eg when the user already played for example a level) we store the level states in a new table called levelsArray.

Now the only thing left is to call the right functions in the play.lua file. Put levels = loadLevels() below your levels table in play.lua

 levels = loadLevels()  

and add in your EnterScene function

saveLevels()

Also don’t forget to require the levelstorage.lua file in your play.lua file.

local levelstorage = require("levelstorage")

Let me know what you think.

3 Responses

  1. seanD November 9, 2013 / 11:56 pm

    thank you so much for your help i finally save my levels and unlock them your a life saver. you should make a tutorial on saving high scores but thanks again.

  2. DS March 24, 2014 / 6:46 pm

    I would also love to see a save highscore tutorial!

  3. Nero January 15, 2015 / 7:41 am

    The only trouble i am having is that it doesnt save the levels when I pass a level, the picture is still a locked picture and the level is not checked that it has passed

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*