How to make a function reset a cooldown every 24hr?

How would I go about doing it? even if it’s pseudocode any ideas would be great.

The first way is to set a timer in your game startup that has an elapsed time of 86400 seconds, and is Looping, and do your thing inside the callback.
This will do the thing whenever the game has been running for 24 hours, 48 hours, …
If I re-start the game in the middle, I have to wait another full 24 hours before it does it again.

The second way is to use the Now and DateTime nodes / functions to get “the current date” (without timestamp.) Compare that to some value you store in a string variable, and if it’s different, update the string variable, and do the thing. Set a looping timer in startup that runs every 10 seconds or whatever, and does this comparison. This will immediately do the thing when you start the game, and then do it every time right after midnight.

The third way: If it’s important that I can stop and re-start the game and still only have the thing happen once a day, then you 'll want to use the second option, but store it in a savegame slot right when you do the thing. Then, make sure you load that savegame slot before you start the timer to check if the date is different.

The fourth way: Require the user to log in to some web service, and use a database to store when it was last run, and tell the game how long it has to wait before it does the thing again. This is the fanciest way to do it, but of course also requires the most infrastructure and code to make happen.

1 Like

Well, if it’s a cooldown, then you just need to get the timespan between current date & time and the last date & time the function ran. If it hasn’t been 24 hours, then don’t run the function. Here’s an example with a cooldown of 5 seconds:


Thank you for your example 2 questions first it’s not a cooldown per se just a variable that needs to be reset daily. so instead of 5 sec I can put 24hr and this will work? basically, I don’t understand the timespan function

Yeah, just put 24hr and it’ll work. The timespan is just the time passed between two dates & times.

Then you can do what jwatte said and just run a function when the full 24 hours has passed. But I think a better way to do it is combine his first and second idea: Set a timer for 24 hours. If the player closes the game before the 24 hours has passed, save the date & time the player quit. When the player starts the game again, get the time that has passed between the last quit and now and then set a timer for the remaining time. This will keep you from constantly having to check all the time (which will always be false until 24hrs has passed).

1 Like

From your example, I decided to do this.

When the profile loads compare today to yesterday
and Save yesterday Instance on Shutdown see a problem with that?

What’s the point?
It’s super easy to cheat this.

You should modify it so that you read out the time from a time serving server.

And even then it’s fairly easy to hijack a DNS locally and feed it your own response.

Like all the mobile games out there that do this thinking people actually wait…

That works. It’ll reset every day at 12:00 AM, but I think that’s what you want.

Yeah, good point. But I think that may be excessive for a spelling game; but you never know.

Thank you

1 Like