Need some pointers, Save Date, Display table, etc.

I am currently developing a game. I am at the point where I am developing the code for scores, stats etc. I need some pointers towards the proper way to do the following.

  • Save high score data to a file that will be stored on the iphone and will remain even after the application is updated to a new version
  • Load the file each time the application is started
  • Reset the data at user request.
  • Display high score data in a table. i.e. doodle jump. With date of record and record info.

-I am also putting the data on OpenFeint and was wondering about the wrapper that is in the code section. I really don’t know what a wrapper does. It seems that it makes it safer. Can someone explain this to me?

Thanks for all the info.
[import]uid: 8192 topic_id: 1962 reply_id: 301962[/import]

Hey Amigoni

Do a search for the json.lua library mentioned in the forums. It will let you encode and save any number of variables. Loading is much the same but in reverse.

If you email me at matt@mattpringle.co.uk I will email you over some code for saving and loading tonight / tomorrow. Clearing the data is easy, you just save a blank file.

Matt
[import]uid: 5354 topic_id: 1962 reply_id: 5810[/import]

Actually had some code on a remote backup server, this saves the scores but you can easily expand.

Im saving 4 scores, thats all in this. Ignore the console.returnScore() as thats a .lua require thats controls a control in the game which displays and tracks the scores. This is a required files as well and I call loadScore and saveScore from a different bit of the app

[code]
– File Score - fileScore.lua
– Version 1
module(…, package.seeall)


– Load Score
function loadScore()
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory )
local file = io.open( path, “r” )
local returnScore

– Load File
if file then
local loadedScores
loadedScores = file:read( “*a” )
local json = Json.Decode( loadedScores )

io.close( file )
if json ~= nil then
returnScores = { json[“loadedScores1”] , json[“loadedScores2”] , json[“loadedScores3”] , json[“loadedScores4”] }
else
returnScores = { 0 , 0 , 0 , 0 }
end
else
returnScores = { 0 , 0 , 0 , 0 }
end

return returnScores[1],returnScores[2],returnScores[3],returnScores[4]

end


– Save Score
function saveScore()

– Load Previous Scores
local loadedScores = {}
loadedScores[1],loadedScores[2],loadedScores[3],loadedScores[4] = fileScore.loadScore()

– Get Current Score
local globalScore = console.returnScore()
table.insert(loadedScores,globalScore)

– Sort Scores
table.sort(loadedScores, function(a,b) return a>b end)

– Make Save String
local saveScores = {
[“loadedScores1”] = loadedScores[1],
[“loadedScores2”] = loadedScores[2],
[“loadedScores3”] = loadedScores[3],
[“loadedScores4”] = loadedScores[4]
}
local json = Json.Encode(saveScores)

– Save File
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory )
local file = io.open( path, “w+” )
file:write( json )
io.close( file )

end

[/code] [import]uid: 5354 topic_id: 1962 reply_id: 5811[/import]

Hi Matthew,

I’m new to Corona / Lua but the posted code looks as one which save local score and not global.

I’m looking for a simple way to save and load data to / from a remote server.
Can you post a sample code which shows how to load/save data on a remote server?
Do you find this approach to be reliable (what happens if your server is down or if your game becomes so popular that you get millions of requests)?

Thanks,
EZ

P.S
Love your work on “Alien Horde” [import]uid: 9536 topic_id: 1962 reply_id: 9499[/import]

You are right that code saves locally. In alien horde I do save the users 4 highest scores locally, the highest is then posted to the server.

The connection only sends a few bytes of data so as long as the iPhone has an internet connection it will only take a few seconds most. If there is no connection then the game just resumes. A really slow connection can hang the game for a few seconds but you get that in all iPhone apps.

I got a server package with 1and1, I use them a lot for my clients ( some big companies ) and they are usually quite good with a solid connection speed. Their 1st level tech support is useless but having a managed server or lower package nothing much can go wrong, and if it does its their end and they know about it.

On the server its php / mysql saving the data and providing the world rank back. ( When AH came out there was no OpenFient )

In lua

-- Build URL  
 local url = "http://www.yoururl.com"  
  
 -- Build POST  
 local post = "action=".."sendingScore".."&score="..score.."&userid="..userID  
  
 -- Send HTTP Request  
 local communication = "false"  
 communication = http.request( url , post )  
 if communication and communication ~= "" and communication ~= "false" then  
 -- Split World Rank And User  
 communication = splitFile( communication )  
  
 -- Save User  
 if communication[2] ~= "" and communication[2] ~= "false" then  
 fileUsers.saveUser( communication[2] )  
 end  
  
 -- Get World Rank  
 if communication[1] ~= "false" and communication[1] ~= "" then  
 worldRank = tonumber( communication[1] )  
 else  
 worldRank = "false"  
 end  
 end  
  
 -- End Alert  
 native.setActivityIndicator( false )  
  
-- Return Communication  
 return worldRank  

1st you need to set the activity indicator on a previous loop otherwise it wont be shown.

2nd you need a url to post data to, you build the post with whatever variables you want to send and off it goes to the server.

On the way back the server sends back “communication” which is a html page with just 1 line of text which I can read back into a lua table

I split the communication into a table, check the values to make sure everything went ok ( checking for false ) and I get the worldRank value the server passed back and I update the local copy on the iphone. worldRank = tonumber( communication[1] )

If you know php or asp then its just a simple case of running a few sql commands to first save, then sort and then count the entries to get ranking.

I could obviously take this much further in storing data, taking payments, anything you can do on a website you could put through the http.request if the server knows enough to process the commands. [import]uid: 5354 topic_id: 1962 reply_id: 9501[/import]

Hi Matthew,

Thanks a lot for the sample code and explanation!
I’ll give it a try and update you.

Thanks,
EZ [import]uid: 9536 topic_id: 1962 reply_id: 9502[/import]