Resume game

Hello again,fellow corona developers)
So, assume i stopped playing the game and exit it through a home button or any other hard button that exits the game… how to save my progress upon this exit or manually and when i start game again i just press “Continue” button in menu screen and game will go off the moment i exited

sorry for,maybe,bad explanation.But i think many stumbled upon this particular problem

any help is appreciated [import]uid: 16142 topic_id: 12111 reply_id: 312111[/import]

Save your game state to a file and read it upon startup. [import]uid: 58455 topic_id: 12111 reply_id: 44171[/import]

how would i do that? is there any tutorial for this? i tried peach pellen tutorial for loading and saving, but it teaching how to save variables, and i need to save everything…

thanks for answer [import]uid: 16142 topic_id: 12111 reply_id: 44180[/import]

“Everything” can be saved with variables. Positions, scores, true or false, user names, etc.

Peach :slight_smile: [import]uid: 52491 topic_id: 12111 reply_id: 44193[/import]

What Peach said. You’re going to have to come up with a file format that makes sense to you, whether it’s XML or CSV or whatever.

I wouldn’t sweat it too much. For most games, you lose your state if you quit in the middle of a level. I think players expect that. Just remember the last level completed so they can start from there.

If it’s good enough for Angry Birds, it’s good enough for my game :slight_smile: [import]uid: 58455 topic_id: 12111 reply_id: 44197[/import]

i have many screens in my app, so i need to restore state of that screen in case of exiting and do it in menu screen…

so i need to state some variable about active my screen or not and then save it and load from menu screen button?

btw,i tried to simplify things and use a CrawlSpaceLib, but that dont work
[lua]local CSL = require(“crawlspacelib”)
CSL.help(“Save”)

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()
local myData = Load()
end

local function onSystemEvent( event )
if( event.type == “applicationExit” ) then
local myData = {myBox.x,myBox.y}

save(myData)
end
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()[/lua] [import]uid: 16142 topic_id: 12111 reply_id: 44205[/import]

I thought this was about saving state when the application exits. You mean when you switch scenes? Like from game to menu and back to game?

Just pause physics and set your display groups’ isVisible property to false, and do the reverse when the game scene is re-entered. If you’re using Director, it may be a problem because I think it blows away everything when you switch scenes.

I wrote my own little SceneManager that swaps between scenes, and it depends on each scene implementing init(), play(), pause() and delete().

[import]uid: 58455 topic_id: 12111 reply_id: 44209[/import]

i think we have a bit of misunderstanding here)
let me ask another way:
how to save my game progress on application exit(or pressing Menu button on screen) and then load this progress with “Continue” button on Main Menu screen?

the problem is that i have many scenes(i.e .lua files), do i need to write save code in all of them?

i really appreciate your answers, this whole save/load thing is melting my brain on this hot days) [import]uid: 16142 topic_id: 12111 reply_id: 44213[/import]

If you need to write file saving and loading code to work around Director’s behavior of blowing away the previous scene when you change to a new one, I’d stop using Director.

But on app exit, you’ll have to save to a file. What you save is up to you. You’ll need to write custom code to write to save, and custom code to restore.

The only things I can think of worth saving on exit are any game settings (sounds on/off, etc.), last level completed, and high scores and achievements if you store those locally and not online somewhere. So maybe you can use a simple text file format like

level=4
sound=on
music=off
scores=dave|1004|mike|994|steve|881

Then you’ll write code to write that file based on current settings, and read and parse it at startup. If you have more settings to save, just add another line to the file.

[import]uid: 58455 topic_id: 12111 reply_id: 44215[/import]

2 ways: Property Bag or Databases.

Yeah? [import]uid: 68047 topic_id: 12111 reply_id: 44229[/import]

if i knew what you mean by that))) i have no idea) [import]uid: 16142 topic_id: 12111 reply_id: 44231[/import]

Well, here you go…

http://developer.anscamobile.com/code/propertybag-class-coronasdk [import]uid: 58455 topic_id: 12111 reply_id: 44232[/import]

Check out the properties class at the link I posted, darkconsoles. Earlier when I talked of writing code to read and write values from file, this is what I was talking about. This person already did the heavy lifting, you just have to use his class.

:slight_smile: [import]uid: 58455 topic_id: 12111 reply_id: 44235[/import]

i will definitely try this, thanks [import]uid: 16142 topic_id: 12111 reply_id: 44239[/import]

so,i tried to use PropertyBag and here is what i wrote so far…and its not working) Terminal is alerting about there is no such thing as defaults directory…

[lua]local prop = require(“property”)

local propertyBag = prop:init()

local ball = display.newCircle(0,0,15)
ball.x = display.contentWidth/2
ball.y = display.contentHeight/2

function moveball(event)
ball.x = event.x
ball.y = event.y
propertyBag:setProperty(“ball.x”, “ball.y”)
end

local function resumeStart()
– restore previous state
propertyBag:GetFromFile()

end

local function onSystemEvent( event )
if( event.type == “applicationExit” ) then
propertyBag:SaveToFile()
end
end

local function init()
– start and resume from previous state, if any
resumeStart()

ball:addEventListener(“touch”, moveball)

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

can anyone post an example of using this PropertyBag? I will really be glad for that,because its so hard to understand whats i need to do with it [import]uid: 16142 topic_id: 12111 reply_id: 44316[/import]

Hmmm…at first glance you appear to be using it correctly. Try putting some print statements in properties.lua and see what it is using for the path name.

I think you missed the point of what a property is, though. The file that is written will be a series of name=value pairs. Yours is going to read ball.x=ball.y, which isn’t want you want. Try propertyBag:setProperty(“BallX”, ball.x).

Also, you probably don’t need to call that every time you move the ball. Just write it once on application exit. [import]uid: 58455 topic_id: 12111 reply_id: 44332[/import]

thanks for advice, but i cant figure out how to make all that damn thing to work, thats my code now:
[lua]local prop = require(“property”)

local propertyBag = prop:init()

local ball = display.newCircle(0,0,15)
ball.x = display.contentWidth/2
ball.y = display.contentHeight/2

function moveball(event)
ball.x = event.x
ball.y = event.y
end
local function resumeStart()
– restore previous state
propertyBag:GetFromFile(“ballX”)
propertyBag:GetFromFile(“ballY”)

end

local function onSystemEvent( event )
if( event.type == “applicationExit” ) then

propertyBag:setProperty(“ballX”, ball.x)
propertyBag:setProperty(“ballY”, ball.y)
propertyBag:SaveToFile()
end
end

local function init()
– start and resume from previous state, if any
resumeStart()

ball:addEventListener(“touch”, moveball)

Runtime:addEventListener( “system”, onSystemEvent )
end

–start the program
init()[/lua]

and thats not working at all, i position ball on screen, then exit app on my android and then puff, everything like at first time, no changes in ball position(

i think i miss something crucial,but dont understand what( [import]uid: 16142 topic_id: 12111 reply_id: 44339[/import]

up [import]uid: 16142 topic_id: 12111 reply_id: 44550[/import]

Fixed.

At first, the exit listener wasn’t firing when I exited the simulator, so I added an os.exit() statement when the ball reached a certain point, just so I could get the listener to fire.

Then I went to where the properties file was allegedly being created, and lo and behold, it was there with updated values:

ballX=280
ballY=367

So why wasn’t it working?

I verified that resumeStart() was in fact getting called, so I scratched my head for a second and then noticed that you’re not setting ball.x and ball.y to the values returned from GetFromFile().

Fixed! Well, not really.

I tried setting them but GetFromFile() was returning nil. And then it dawned on me that GetFromFile() doesn’t sound like the right function to be calling to get a property. GetFromFile(), as it turns out, just loads the previously saved property file. The function to get an individual property from the loaded properties is getProperty().

That is the function you need to be calling, after GetFromFile(), to set ball.x and ball.y.

I got this far by putting print statements in your code and in properties.lua, and also by digging into properties.lua to see what it is really doing. After a few iterations of trial and error, I fixed it.

It’s called debugging. :-/ [import]uid: 58455 topic_id: 12111 reply_id: 44551[/import]