I’m working on a game who has an energy system, like those from social games. I wanted to make a timer to fill the energy bar after given amount of time. I also want that this timer to count even if the app is closed, so the user can exit the game when the energy runs out, and when he reopen the game, after some time, the energy bar will be filled based on the waiting time. Here is the problem, I have no clue how to do that … Can you give me some hints?
Best way is to get the current timestamp from a server and then subtract the difference from last time running. Otherwise user can change their system time to time travel. Try this
Yes, using a server timestamp prevents cheating, but is that really a big problem? Not all users will do this and it is unlikely that a large portion of those users would be paying users anyway (if you monetize these timers using IAPs).
If you want a simple offline version then I suggest something like this (over simplified, you’ll need to adapt to your game obviously):
-- get current time and store this in a file
-- do this when the timer starts
local timers = {
timer1 = os.time(),
}
sys.save(sys.get_save_file("mygame", "timers"), timers)
-- read time when the app starts
-- update in-game timer
local timers = sys.load(sys.get_save_file("mygame", "timers"))
if timers.timer1 then
print("Timer was started at ", timers.timer1)
end
If the game is casual it matters less. If the game is core it matters more.
If you monetize on IAP it matters less. If you monetize on ads it matters more.
If your game is single player focused it matters less. If your game has a community in some way it matters more.
I’d trust local clock for so many hours. Maybe 48 hours. Then require trusted server timestamp. And don’t allow time travel for more than 72 hours without a server check. But it depends on the game and if it really matters to monetization or not.