How to set a timer that will operate even when app is not running?

When making a mobile app, how do I create a timer that will run in real time? I am wanting to set up a life system that will add a new life after so much time. But, I don’t want the app to have to remain open in order for the timer to run. How would I set this up?

Hi,

I don’t think that doing anything when the app is not running is possible through Blueprints. A possible solution I can imagine is that you save the state of the timer when the player closes the app to a database, and when he opens the app again, you query the database. If you don’t want to go online, you could save the timer state along with the other variables you save about the player.

[EDIT]

Reading through your question again, what I provided is not eaxactly what you want to do, sorry. However it can still work if you save the actual date and time and then with some math magic you calculate the time passed since logout, so you can also calculate how many life the player should have received in that time period.

Just to be sure I understand what you mean, let’s say it takes ten minutes (real time) to get back one life. The life timer begins as soon as the one life is spent, lets say at 1:00pm. I would then tell the bp to refill the life no later than 1:10? And do this through the game state?

No, because if your app is not running the Blueprint can’t check what the time is.

What I said, is that you save something like this when the player closes the app:

“CurrentLifesWhenAppClosed”, “DateTimeOfAppClosed” (the first one stores how many lifes the player has, and the second one stores the date and time when the player closed the app)

Upon opening the app, you want to load up these variables from your save, and then do some math with it.

First, you check if the player closed the app with maximum lifes or not (checking if “CurrentLifesWhenAppClosed” is the maximum that the player can have) . If not, you want to do something like this:

You check the “DateTimeOfAppClosed” and compare it to the current date and time. This way you can get exactly how much time passed since you closed the app last time. You take the result, convert it to minutes (let’s say the app was closed for an hour=60minutes), and you divide it by 10 (10 if you want to give a life to the player every 10 minutes). In this case we would get 6. So we know, that since the last time the player played he gained 6 lifes, now you can add this to your Current Life variable.

Hope this clears it up a bit.

For the timing when the player actually plays, you can setup a simple 10 minute timer which starts to count down every time a life was given (and the player doesn’t have max life)

Kontur’s solution seems like the way to go. Rama’s plugin should help greatly with implementing it too:

You should be able to use the first node in that post (get real world current OS time), and save the string you get to hard drive, when the player exits the game.

Upon re-launching the game you can load that string and use the second node to find out how much time passed, in minutes/seconds/whatever.