How to make a timer to run if app is suspended

Monkey Forums/Monkey Programming/How to make a timer to run if app is suspended

Raul(Posted 2014) [#1]
For my type of game, I need to continue run a timer (to refill the player energy) even if this one suspended the application from the iPad or Android Hoem button.

I am using the small cDeltaTimer class, but the update is done in OnUpdate.

How should I handle this?


Gerry Quinn(Posted 2014) [#2]
Well, you can check Millisecs() when an update finally occurs, and update the energy then based on the total time elapsed I.e. if the last update was 15 minutes ago, give a full 15 minutes of energy, even if nothing else gets 15 minutes worth of updates.


Raul(Posted 2014) [#3]
Well, I made this test:
	Method OnResume:Int()		
		Print "resumed @ " + Millisecs()'
	End
	
	Method OnSuspend:Int()
		timerSuspended = Millisecs()
		Print "suspend @ " + timerSuspended
	End


but the Millisescs() is returning the actual app time so when i come back is still the same time as when I leave..


nikoniko(Posted 2014) [#4]
Raul wrote:
but the Millisescs() is returning the actual app time so when i come back is still the same time as when I leave..


Calculate time to full refill, store it and check time to up in update loop.


Raul(Posted 2014) [#5]
@nikoniko:
Lets say the time to refill is 60 seconds. When the users suspends the application I store this value, right?

Second time when he enters, how do I know how many seconds pasts since last check?


Ferdi(Posted 2014) [#6]
GetDate function in mojo.app. Is that what you are looking for?


Gerry Quinn(Posted 2014) [#7]
I didn't realise that Millisecs() stopped when the app was suspended. But as Ferdi says, GetDate() is therefore the answer.


Raul(Posted 2014) [#8]
Yeah! GetDate()

Thanks guys :D

Love you all!


Ferdi(Posted 2014) [#9]
Just a warning in GLFW the millisecs part is not working in GetDate. It works in HTML5.


Raul(Posted 2014) [#10]
Thanks for sharing that Ferdi, but I only need this for iOS and Android.