+3 votes

I would like to make a timer that runs out even if I switch the screen off or use another app once the timer started. Is it possible somehow? thanks guys

in Engine by (49 points)

I also need the answer to this question.

same here I need answers as well

1 Answer

–1 vote
var wait_time := 3600 # how long the timer should last, in seconds (this example is 1 hour)
var start_time := OS.get_unix_time() # set this value to `OS.get_unix_time() whenever you want to reset the timer

(below would go in _process() or _physics_process())

var time_left := start_time + wait_time - OS.get_unix_time()
if time_left < 0:
    # put code to run when timer ends here

You would probably want to store the start_time value in the disk as well in case the app gets closed by the device to save memory while the user is doing something else
One caveat is that it's dependant on the device's time being accurate and that the user isn't changing it. If the user changes their device's time, it can trick the app into thinking it actually is whatever time they set it to
Also the timer might run out while the user isn't using the app so keep that in mind and take that into account when you program whatever happens when it runs out.

by (32 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.