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]