Save and Restore State

Posted by Gilbert, Posted on July 21, 2010, Last updated October 7, 2010

9 votes

This sample code shows how to save values to a data file and use them to restore the state of an object when the app runs again.

There is a yellow box in the middle of the screen. Move it and then close the app. Open the app again and it will be where you last left it.

local myBox = display.newRect(0, 0, 40, 40)
myBox:setFillColor(255, 255, 0)
 
local function moveBox( event )
        myBox.x = event.x 
        myBox.y = event.y 
end
 
local function resumeStart()
                -- restore previous state
                local path = system.pathForFile( "data.txt", system.DocumentsDirectory )                
                local file = io.open( path, "r" )
                
                if file then
                        print("loading previous state variables...")
                        local contents = file:read( "*a" )
                
                        -- separate the variables into a table using a helper function
                        local prevState = explode(", ", contents)
                        
                        myBox.x = prevState[1]
                        myBox.y = prevState[2]
                                                                                
                        io.close( file )
                else 
                        myBox.x = display.stageWidth/2
                        myBox.y = display.stageHeight/2
                end
end
 
local function onSystemEvent( event )
        if( event.type == "applicationExit" ) then              
                -- create a file to save current state
                local path = system.pathForFile( "data.txt", system.DocumentsDirectory )                
                local file = io.open( path, "w+b" )
                
                -- save current state variables in comma separated list
                file:write( myBox.x ..", ".. myBox.y )          
                io.close( file )
        end
end
 
-- explode helper function
function explode(div,str)
  if (div=='') then return false end
  local pos,arr = 0,{}
  -- for each divider found
  for st,sp in function() return string.find(str,div,pos,true) end do
    table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
    pos = sp + 1 -- Jump past current divider
  end
  table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  return arr
end
 
local function init()
        -- start and resume from previous state, if any
        resumeStart()   
        
        myBox:addEventListener("touch", moveBox)
 
        Runtime:addEventListener( "system", onSystemEvent )     
end
 
 
--start the program
init()

Compatibility: 
Corona 1.0

Replies

amigoni
User offline. Last seen 5 weeks 12 hours ago. Offline
Joined: 12 Aug 2010

Thanks for this useful code. Will this information stay saved during and after an update of an application?

MichaelAssadi
User offline. Last seen 1 day 20 hours ago. Offline
Joined: 20 Jan 2011

Yes? Will this still be saved during and after an update ?

leo.artfinal's picture
leo.artfinal
User offline. Last seen 12 weeks 19 hours ago. Offline
Joined: 20 Apr 2011

Same big one question! It will save the data after an update, for example?

Great work!

mightE
User offline. Last seen 2 weeks 2 days ago. Offline
Joined: 27 Jul 2011

Very clever!

The question above is a very good one.

paingpyi
User offline. Last seen 14 weeks 1 day ago. Offline
Joined: 16 Sep 2010

Is there any better way to restore whole game screen? If we have over 10 animations are running and timers, do we have to store all frame numbers and timers? It doesn't make sense anymore :( I just want to be as it is after leaving the screen for other apps and come back later. Magic Defenders may found the better way but have no idea how they managed. If you look carefully, all small/big animations and position are exactly as I left.

Joe Kool
User offline. Last seen 5 weeks 20 hours ago. Offline
Joined: 1 Jun 2011

is there a way to make this save a score?