Saving Game Progress

7 replies [Last post]
werdinteractive's picture
werdinteractive
User offline. Last seen 4 days 22 hours ago. Offline
Joined: 11 Feb 2010

This code is covered in the API Reference Guide, but I have never found it in the forums so I thought I would offer it to the community and add some context to it.

-- This code goes on your NewGameFunction for your New Game button
local path = system.pathForFile( "UnlockLevelProgress.txt", system.DocumentsDirectory )
file = io.open( path, "w+" ) local UnlockedLevelData = 1
file:write(UnlockedLevelData)
io.close( file )

-- This code goes on your Load Game Button
file = io.open( path, "r" )
if file then
-- read all contents of file into a string
UnlockedLevelContent = file:read( "*a" )
io.close( file )
RemoveMainMenu()
LevelSelectionScreen()
else
NewGameFunction() -- A dialog box can also be prompted here to let the player know that they have no saved progress
end

-- This code goes at the end of your level 1st level
local path = system.pathForFile( "UnlockLevelProgress.txt", system.DocumentsDirectory )
file = io.open( path, "w+" ) local UnlockedLevelData = 2
file:write(UnlockedLevelData)
io.close( file )

-- This code goes in your level selection screen
if (UnlockedLevelContent == "1" or UnlockedLevelContent == "2") then
-- Insert the code for your next button on your level selection screen to switch to level 2
end

Replies

Matthew Pringle
User offline. Last seen 3 weeks 2 days ago. Offline
Joined: 23 Feb 2010

Thanks for that, I was just about to implement a similar system and you've saved me a bit of time.

Cheers

Matt

jtlaird
User offline. Last seen 2 weeks 2 days ago. Offline
Joined: 30 Apr 2010

Thanks for this. Has anyone expanded this example (or done another) that includes saving player state on app suspend or exit? It's a pretty important thing to add to any significant game, and I can't find a working example anywhere. My example from the API documentation doesn't yet work, but if I get it working, I'll post it. In the meantime, any volunteers?

fpassa's picture
fpassa
User offline. Last seen 20 hours 27 min ago. Offline
Joined: 17 Sep 2009

jtlaird,

I just modified this version practicaly 80%. I had to use JSON to store Hi-Score table, so I made all the changes needed to store that information. In fact, my game Ultimate Coins Game version 2.0 uses this modified version.

The reason to use JSON library, is because the information needed to be stored and then loaded. I realized that serializing method is needed to store/load all the information regarding player statistics, like (in my case) track the Score, Silver, Golds, Time, Level and Bonus information.

But I guess, depending on your needs, you can use it as is. My recommendation: if you need to store additional data like me, jump to use JSON library. If you only wish to store a simple data, you can use it as is.

Flavio.

Matthew Pringle
User offline. Last seen 3 weeks 2 days ago. Offline
Joined: 23 Feb 2010

What you could do is store the settings / high scores as 1 long string...

for example I would save the high scores as 1034-1324-1575-1643 and then when loading i would explode the string on the "-" (explode is a php terms, cant remember the lua one for separating a string into an array). Once exploded you get an array of values, which if you know the order of the that those values are saved in then you can reassign them back to the original variables

then loadedHighScore[1] would be 1034, loadedHighScore[2] would be 1324 etc...

If you know all the variables you need to save then loading them back in is easy.

s.w.powers
User offline. Last seen 1 year 4 weeks ago. Offline
Joined: 16 Mar 2010

Use json.

Elegant and easy.

This is the Json lib I use: http://www.chipmunkav.com/downloads/Json.lua

It contains the documentation. Very quick and easy to use, typically used for web transport, but also makes it really easy to use for saving options in a table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
local t = {
      ["name1"] = "value1",
      ["name2"] = {1, false, true, 23.54, "a \021 string"},
      name3 = Json.Null()
} 
 
local json = Json.Encode (t)
 
print (json)
 
--> {"name1":"value1","name3":null,"name2":[1,false,true,23.54,"a \u0015 string"]} 
 
local t = Json.Decode(json)
 
print(t.name2[4])
 
--> 23.54

Oh and in the example, you don't need to use Json.Null(), that's just showing you how to write a nil out.

At any rate, encode the table, write it to your options file, now it's saved.

Read it back in, decode it, now it's available to you in the table var.

Scott

jtlaird
User offline. Last seen 2 weeks 2 days ago. Offline
Joined: 30 Apr 2010

Thanks for the examples, folks, and I'm sure they will help lots of folks to understand better how to "save" data.

However, I should have been clearer in my question. The data saving part I have no problem with. My problem is actually catching the "events" that fire off the data saving and loading -- i.e., app suspend, app exit, app resume, app start. Even though I've followed examples for registering and using other kinds of events (such as button and time event) with no problem, I can't find any examples for using these four events, and they seem to not work the same as the others...

I've tried writing my handler for one of these events as a function called "appListener" and then linking the event to it with:

Runtime:addEventListener("applicationResume", appListener);

but no luck!

Anyone care to take a stab at that part of the challenge? Thanks!

MikeHart
User offline. Last seen 1 week 2 days ago. Offline
Joined: 22 Mar 2010

Here is how I do it:

--****************************************************************
local onSystemEvent = function( event )
--****************************************************************
if event.type == "applicationExit" then
SaveScores()
SavePlayerName()
elseif event.type == "applicationStart" then
LoadScores()
LoadPlayerName()
elseif "applicationSuspend" == event.type then
tSuspend = system.getTimer()
elseif "applicationResume" == event.type then
tPrevious = tPrevious + ( system.getTimer() - tSuspend )
end
end
Runtime:addEventListener( "system", onSystemEvent )

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.