Tips for saving app state?

I’m working to figure out how I’m going to implement the ability to pause, save and load the state of my game. The Jon Beebe tutorial from a few weeks ago is exceptionally enlighting on the “big picture” of implementing this:

http://www.coronalabs.com/blog/2012/07/17/using-system-events-to-save-app-state/
Does anyone have any thoughts regarding the “nuts and bolts” of pausing, saving and loading? I plan on using an onSystemEvent() function much as he shows in his article:
[lua]local function onSystemEvent( event )
if (event.type == “applicationExit”) then

save_state()

elseif (event.type == “applicationOpen”) then

load_saved_state()

elseif (event.type == “applicationSuspend”) then

pause_game()

end
end
Runtime:addEventListener( “system”, onSystemEvent )[/lua]

I guess I should be using tables to track each type of “thing” I have in my game (the Player, NPCs, projectiles, the map, etc.)? Save the tables on suspend or exit and then reload them when resumed/reloaded?

I currently have a table that houses all of my NPCs. I’m guessing I should have one for projectiles as well? Basically any “thing” I have in the game?

I plan on using storyboard for transitioning from screen to screen (still just building my base systems). Are there any considerations I should keep in mind with the Storyboard API and pausing/saving/loading?

Are there any sample games that would be best to look at? I currently don’t have a “game loop” in my game. It seems like having one might make this easier. Any thoughts?

Thanks for your help in getting a better grasp of how to manage this. [import]uid: 105707 topic_id: 29341 reply_id: 329341[/import]

You could try this:

What you’d need to do for this idea is add this function to Satheesh’s saving lib:
[lua]function preference.saveCustom(params)
local fileName = params.fileName
local value = params.value
saveModule(fileName,value)
end[/lua]
The good thing about Satheesh’s saving and loading module is you can save tables.
For example, in your “NPC” table you create an NPC.
[lua]local npc={}
local npc[1]=display.newImage(“NPC.png”) – You should probably use a for…do loop, but I’m just giving an example
npc[1].health=500 – I’m just making some random values
npc[1].stamina=25
npc[1].ammo=150[/lua]
And then you could make your character’s values:
[lua]local player=display.newImage(“Heroic_Guy.png”)
player.health=1250
player.stamina=50
player.ammo=500[/lua]
Then, for the saving function:
[lua]local function saveState()
for i=1, #npc do
saver.saveCustom{value={npc[i].health, npc[i].stamina, npc[i].ammo}, fileName=“npcSavedState”…i…".txt"}
end
saver.save{amountOfNPCs=#npc} – Save the amount of NPCs on your screen
saver.save{levelToContinueFrom=currentLevel} – Save your other values
saver.save{playerState={player.health, player.stamina, player.ammo} – Save your player’s state
end[/lua]
And for the load saved state function:
[lua]local function loadState()
storyboard.gotoScene(“level”…saver.getValue(“levelToContinueFrom”))
for i=1, saver.getValue(“amountOfNPCs”) do
local npcState=saver.getValue(“npcSavedState”…i)
npc[i]=display.newImage(“NPC.png”)
npc[i].health=npcState[1]
npc[i].stamina=npcState[2]
npc[i].ammo=npcState[3]
playerState=saver.getValue(“playerState”)
player=display.newImage(“Heroic_Guy.png”)
player.health=playerState[1]
player.stamina=playerState[2]
player.ammo=playerState[3]
end[/lua]
That’s probably not very efficient, but it’s a starting point.
[import]uid: 147322 topic_id: 29341 reply_id: 117926[/import]

@Binc - I appreciate your input and example code. I’ve been thinking a lot about what you’ve shared and think I have some ideas on how I’ll attempt this.

The whole issue of app state seems central to any project and in the future I’m definitely designing my project “architecture” in the future around this.

If I save a table at a point in time, wouldn’t it save the values within the table at that time? So if my game functions are updating appropriate table values (".x" and “.y” by default, custom values such as a a “.logicstateflag” to track what action an npc is doing, etc.) I would be set, wouldn’t I?

Thanks :slight_smile: [import]uid: 105707 topic_id: 29341 reply_id: 118074[/import]

Yes, it does save it at that time. If you’ve already saved your NPC earlier, it will overwrite the previous time with the new info. For example, if you have your NPC’s ammo full, and then you save, and then it uses some, and then you save, it will save the new ammo over the old one.

Keep in mind, though, that if you change a value, it will not re-save it automatically. You’d need to manually save it again. [import]uid: 147322 topic_id: 29341 reply_id: 118149[/import]

I had to change my name… Thinking of when I make my first million dollars :slight_smile: and Microsoft Bing sues me.

quinc [import]uid: 147322 topic_id: 29341 reply_id: 118190[/import]