Android Do not stop timers when game is minimized

Vallo92

Member
There is a timer in my game. Today I realized that if the game is run on a mobile device, and during the game the game is minimized (by pressing the home button), the timer is paused and does not continue in the background. I need the timer to continue equally even if the game is minimized.
Can anyone tell me how I can do it?
 

Vallo92

Member
You can use time functions to calculate the elapsed time instead of alarms:
https://docs.yoyogames.com/source/dadiospice/002_reference/date and time/index.html
Thanks for the reply. I only had one idea, but I don't know how to make it work properly.
I thought of saving in a variabele the value of "current_time", when the player presses the home button, to put the app in the background. The fact is that I don't know how to detect when the app is "activated" to save the new "current_time" in another variable. and then compare it to the previous one.
Can anyone help me? Or is there a simpler and more functional method?
 

Vallo92

Member
I did not know this function, it seems very useful for what I need, but it is still not clear to me how to understand if the app is reopened after being minimized (to then calculate the time passed).
 

TheouAegis

Member
Is this a timer that is going to function like in many, many mobile games where the player starts crafting something and then a timer shows him how long he has, so then the player can even turn off his phone and go about his daily life, and then when it comes back the crafting will be done? In that case, it doesn't matter if the game has focus or not. You just set the time that the crafting started and then you check if the current time minus the time that the crafting was started is greater than or equal to the amount of time required.
 

Vallo92

Member
Is this a timer that is going to function like in many, many mobile games where the player starts crafting something and then a timer shows him how long he has, so then the player can even turn off his phone and go about his daily life, and then when it comes back the crafting will be done? In that case, it doesn't matter if the game has focus or not. You just set the time that the crafting started and then you check if the current time minus the time that the crafting was started is greater than or equal to the amount of time required.
I try to explain myself better: mine is a turn-based game (a simple card game). Each player has 30 seconds to complete his turn. At the end of the 30 seconds the turn automatically passes to the next player. I wanted to make sure that if one player minimizes the app, the other player won't have an infinite wait, but the turn time will pass anyway, and the time will be updated even if the app was kept in the background.
 

Tsa05

Member
The time functions linked in an earlier post will be great.
When a player submits a move, the current time is recorded.
When a player receives a submitted move, the current time is recorded.

In this way, both players know what time it is when the turn changed.
Every step, check whether 30s have passed since the recorded time.

If you're making a turn-based multiplayer game, you'll have to include an extra factor in here, since there could be lag between sending and receiving. But that works the same way--after 30s have passed, keep checking the time until some extra "permitted" several seconds have passed. If there's still been no communication resetting the timer, change turns.
 

Vallo92

Member
The time functions linked in an earlier post will be great.
When a player submits a move, the current time is recorded.
When a player receives a submitted move, the current time is recorded.

In this way, both players know what time it is when the turn changed.
Every step, check whether 30s have passed since the recorded time.

If you're making a turn-based multiplayer game, you'll have to include an extra factor in here, since there could be lag between sending and receiving. But that works the same way--after 30s have passed, keep checking the time until some extra "permitted" several seconds have passed. If there's still been no communication resetting the timer, change turns.
Check every single step of the game past time, does it weigh down the execution of the game? I thought it was possible to solve my problem by using a single check when the app was minimized and when it was opened to update the elapsed time.
 

Tsa05

Member
@Vallo92 Don't worry about it. Comparing 2 numbers once per game step isn't going to be a problem.
Think about this: if you were not concerned by minimizing/closing the game, you'd *still* be keeping track of the turn timer, and GameMaker would be changing the timer every step to see if an alarm has triggered, right?

But since alarms can't trigger when the game isn't active, you compare the current time with the "start time" of the turn every step, instead of subtracting one from the timer and comparing with zero every step.
 
Top