How to make a timer that works out of the game?

Hi guys, here I am again with a question.

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?

1 Like

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

2 Likes

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
2 Likes

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.

3 Likes

Would you still recommend this method @Pkeod ?

Check out this dedicated tool for this use case.

4 Likes

Thanks. Saw this a few minutes after my comment.

2 Likes