data changes while game is inactive

Hi, everybody

I’m working on a virtual pet (tamagotchi that kind of stuff) application. And I’m out of ideas on how to realize one thing. I got a little help on stackoverflow, but not enough unfortunately:)
Now that I finally am able to post here, I think someone probably could help me.
the question is: how can I make the pet change its status (go hungry, die etc) while the game is out?
As I was answered I need to make some saves into a txt save file with the “time since played” and than when player enters game to load it, make some calculations and change the pets status on-fly.
OK, I get idea but really can’t understand the realization of it (what api, etc).
Any help would be very much appreciated!

Thanks,
Greg [import]uid: 161390 topic_id: 28757 reply_id: 328757[/import]

Oops wrong place to post it, I guess. Please move it if possible [import]uid: 161390 topic_id: 28757 reply_id: 115901[/import]

This is how you might do it if you use the Ice library: https://github.com/GrahamRanson/Ice

You’d have something like this when your app first launches:

[lua]require(“ice”)

local saveGame = ice:loadBox(“saveGame”) – creates ‘icebox’ if one does not exist
local exist = saveGame:retrieve(“exist”) – checks whether the exist flag set within icebox
local hunger
local lastPlayed

if exist == nil then – if icebox didn’t exist, set up some variables and store

saveGame: store(“hunger”,0)
saveGame: store(“lastPlayed”,0)
saveGame: store(“exist”, 1)
saveGame: save()

else – if it did exist, retrieve the pet’s hunger and the time the game was last played

hunger = saveGame: retrieve(“hunger”)
lastPlayed = saveGame: retrieve(“lastPlayed”)

end[/lua]

When the game exits, you would do something like this:

[lua]local t = os.time()
saveGame:store(“hunger”,hunger)
saveGame:store(“lastPlayed”, t)
saveGame:save()[/lua]

You might want to set up something that saves this value periodically, so if the game exits abnormally you have a value saved close to when the game crashed or the user force-closed or something.

Then calculate how hungry the pet should be given the time spent outside the game:

[lua]local calculateHunger = function ()

local timeNow = os.time()

local secondsElapsed = timeNow - lastPlayed

hunger = hunger + secondsElapsed/60 – adds a hunger point for every minute the game was closed

end[/lua]

Then just set up a function which calculates what should happen depending on the value of ‘hunger’, which I guess would be part of your gameLoop anyway.

[import]uid: 93133 topic_id: 28757 reply_id: 115904[/import]

You can also add local notifications to schedule messages like “your pet is very hungry.” That will bring the user back into the app and if he goes to the app right away, you can implement nick’s code above, or if even more time passed since he did, you could make the pet sick or die. [import]uid: 6084 topic_id: 28757 reply_id: 115905[/import]

wow! didn’t expect such a thorough and detailed reply!
Huge thanks, Nick!

BeyondtheTech, also thank you for the good idea! [import]uid: 161390 topic_id: 28757 reply_id: 115909[/import]

No problem - I can’t work on my projects while I’m at work so this is the next best thing!

[import]uid: 93133 topic_id: 28757 reply_id: 115911[/import]