Save game

How to save game score and save the player position when leave the game ?

You have 2 options:

  1. Save the data locally to a file (I normally use JSON files, but you could also use SQL).

http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/

  1. Send data to a server. This would involve you having server space of some kind, and most likely needing knowledge of PHP/Ruby etc in order to send data from the app to a server, store in a database, and retrieve when needed.

Option 1 is certainly easier. The only real downside is that if the user uninstalls the app then they lose the save data. The OS will give them a warning of this though, and it’s not really unreasonable to lose your data if you uninstall.

I’ll give you some save/load functions if you want to use json:

function load\_settings() local path = system.pathForFile( "YOUR\_FILE.json", system.DocumentsDirectory ) local file = io.open( path, "r" ) local contents if file then local saveData = file:read( "\*a" ) io.close( file ) local contents = json.decode(saveData) else --if file doesn't exist then check resource directory local path2 = system.pathForFile( filename, system.ResourceDirectory ) local file2 = io.open(path2, "r") if file2 then contents = file2:read( "\*a" ) io.close (file2) end end return contents end --saveData is a table of things you want to save function save\_settings(saveData) saveData = json.encode(saveData) local path = system.pathForFile( "YOUR\_FILE.json", system.DocumentsDirectory ) local file = io.open( path, "w" ) file:write( saveData ) io.close( file ) file = nil end

Obviously “YOUR_FILE.json” should be renamed to whatever you want (“settings.json”, “scores.json” etc).

I usually put a default version of each json file in the main directory of the project. That way I can look for saved data in the documents directory, and if none are found I can use the default data instead. My load function already does that for you (as long as you put the file into the resource folder).

How to use the save/load functions

function load\_settings() local path = system.pathForFile( "YOUR\_FILE.json", system.DocumentsDirectory ) local file = io.open( path, "r" ) local contents if file then local saveData = file:read( "\*a" ) io.close( file ) local contents = json.decode(saveData) else --if file doesn't exist then check resource directory local path2 = system.pathForFile( filename, system.ResourceDirectory ) local file2 = io.open(path2, "r") if file2 then contents = file2:read( "\*a" ) io.close (file2) end end return contents end --saveData is a table of things you want to save function save\_settings(saveData) saveData = json.encode(saveData) local path = system.pathForFile( "YOUR\_FILE.json", system.DocumentsDirectory ) local file = io.open( path, "w" ) file:write( saveData ) io.close( file ) file = nil end gameScore = 0 player = display.newRect(100,100,100,100) function move(e) if e.phase =="ended" then player.y = player.y + 20 gameScore = gameScore + 1 score.text = gameScore end end score = display.newText( gameScore , 10, 80, "Helvetica", 50 ) player:addEventListener("touch",move)

It may not be what you want to hear, but I’d thoroughly suggest reading up about Lua and the Corona SDK beforehand.

I have try but still can’t work . Can you check my code please ?

loadsave.lua

-- load the JSON library. local json = require("json") -- Function to save a table.  Since game settings need to be saved from session to session, we will -- use the Documents Directory local json = require("json") function saveTable(t, filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) return true else return false end end function loadTable(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end 

main.lua

local loadsave = require("loadsave") gameScore = 0 player = display.newRect(100,100,100,100) function move(e) if e.phase =="ended" then player.y = player.y + 20 gameScore = gameScore + 1 score.text = gameScore end end score = display.newText( gameScore , 10, 80, "Helvetica", 50 ) player:addEventListener("touch",move) myTable = {} myTable.score = gameScore saveTable(myTable, "myTable.json") myTable = loadTable("myTable.json")

I suggest reading up on Lua errors - how is anybody supposed to know (beyond basic Syntax errors) what the problem is without a proper report, it’s impossible to diagnose because you’re not providing the correct information.

There is a blog post on how to debug, I’m not going to provide the link because I believe that’s half the problem - it will benefit you greatly if you know how to search/find what you’re looking for.

You say it doesn’t work, do you have any error messages or anything at all that can help us to work out where your problem lies?

I’ve been using those functions for about 2 years, so I know they definitely work.

You have 2 options:

  1. Save the data locally to a file (I normally use JSON files, but you could also use SQL).

http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/

  1. Send data to a server. This would involve you having server space of some kind, and most likely needing knowledge of PHP/Ruby etc in order to send data from the app to a server, store in a database, and retrieve when needed.

Option 1 is certainly easier. The only real downside is that if the user uninstalls the app then they lose the save data. The OS will give them a warning of this though, and it’s not really unreasonable to lose your data if you uninstall.

I’ll give you some save/load functions if you want to use json:

function load\_settings() local path = system.pathForFile( "YOUR\_FILE.json", system.DocumentsDirectory ) local file = io.open( path, "r" ) local contents if file then local saveData = file:read( "\*a" ) io.close( file ) local contents = json.decode(saveData) else --if file doesn't exist then check resource directory local path2 = system.pathForFile( filename, system.ResourceDirectory ) local file2 = io.open(path2, "r") if file2 then contents = file2:read( "\*a" ) io.close (file2) end end return contents end --saveData is a table of things you want to save function save\_settings(saveData) saveData = json.encode(saveData) local path = system.pathForFile( "YOUR\_FILE.json", system.DocumentsDirectory ) local file = io.open( path, "w" ) file:write( saveData ) io.close( file ) file = nil end

Obviously “YOUR_FILE.json” should be renamed to whatever you want (“settings.json”, “scores.json” etc).

I usually put a default version of each json file in the main directory of the project. That way I can look for saved data in the documents directory, and if none are found I can use the default data instead. My load function already does that for you (as long as you put the file into the resource folder).

How to use the save/load functions

function load\_settings() local path = system.pathForFile( "YOUR\_FILE.json", system.DocumentsDirectory ) local file = io.open( path, "r" ) local contents if file then local saveData = file:read( "\*a" ) io.close( file ) local contents = json.decode(saveData) else --if file doesn't exist then check resource directory local path2 = system.pathForFile( filename, system.ResourceDirectory ) local file2 = io.open(path2, "r") if file2 then contents = file2:read( "\*a" ) io.close (file2) end end return contents end --saveData is a table of things you want to save function save\_settings(saveData) saveData = json.encode(saveData) local path = system.pathForFile( "YOUR\_FILE.json", system.DocumentsDirectory ) local file = io.open( path, "w" ) file:write( saveData ) io.close( file ) file = nil end gameScore = 0 player = display.newRect(100,100,100,100) function move(e) if e.phase =="ended" then player.y = player.y + 20 gameScore = gameScore + 1 score.text = gameScore end end score = display.newText( gameScore , 10, 80, "Helvetica", 50 ) player:addEventListener("touch",move)

It may not be what you want to hear, but I’d thoroughly suggest reading up about Lua and the Corona SDK beforehand.

I have try but still can’t work . Can you check my code please ?

loadsave.lua

-- load the JSON library. local json = require("json") -- Function to save a table.  Since game settings need to be saved from session to session, we will -- use the Documents Directory local json = require("json") function saveTable(t, filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) return true else return false end end function loadTable(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end 

main.lua

local loadsave = require("loadsave") gameScore = 0 player = display.newRect(100,100,100,100) function move(e) if e.phase =="ended" then player.y = player.y + 20 gameScore = gameScore + 1 score.text = gameScore end end score = display.newText( gameScore , 10, 80, "Helvetica", 50 ) player:addEventListener("touch",move) myTable = {} myTable.score = gameScore saveTable(myTable, "myTable.json") myTable = loadTable("myTable.json")

I suggest reading up on Lua errors - how is anybody supposed to know (beyond basic Syntax errors) what the problem is without a proper report, it’s impossible to diagnose because you’re not providing the correct information.

There is a blog post on how to debug, I’m not going to provide the link because I believe that’s half the problem - it will benefit you greatly if you know how to search/find what you’re looking for.

You say it doesn’t work, do you have any error messages or anything at all that can help us to work out where your problem lies?

I’ve been using those functions for about 2 years, so I know they definitely work.