The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

Hi everyone,

I'm seraching for a way to have a signal emitted on reaching a certain OS-time...

I have two Labels, an hour-Label and a minute-Label, which contain the numbers the user enters, e. g. "08" and "35". Perhaps a way could be comparing these numbers to the actual according OS-time numbers (preferably once every second, not necessarily on every frame) and then trigger if they match.

Would that be a good approach? Or is there a better way? (I'd assume this was a popular feature but I couldn'd find any example.)

Godot version 3.3
in Engine by (525 points)

2 Answers

0 votes

You could use OS.get_datetime(). Here's what the docs say about it:

Dictionary get_datetime ( bool utc=false ) const

Returns current datetime as a dictionary of keys: year, month, day, weekday, dst (Daylight Savings Time), hour, minute, second.

Then emit your signal when it returns the time you want.

by (2,018 points)

This would be perfect. But how could I tell Godot my time of choice?
I only used OS.get_datetime() for displaying the current year, month, day and time etc. by now.

0 votes

golddotasksquestions on reddid suggested for "trigger something exactly at a specific OS-time, down to a specific second":

var alarm_has_not_triggered = true

func _process(delta):
    var time = OS.get_time()
    var time_string = str(str(time.hour).pad_zeros(2),":",str(time.minute).pad_zeros(2),":",str(time.second).pad_zeros(2))
    if time_string == "17:15:30" and alarm_has_not_triggered:
        print("The Time Is Now")
        alarm_has_not_triggered = false

and for "trigger something repeatedly every second, in sync with OS-time-second":

var last_second = OS.get_time().second

func _process(delta):
    var time = OS.get_time()
    if time.second != last_second:
        print("start of a new second")
    last_second = time.second

which work like a charm.
(I added some "returns" not to have the time printed on each frame.)

by (525 points)

Just wanted to add here for the "trigger every second" and this depends on your usecase ofcourse.
If the hour and minute your user can enter is far in the future (like say 10 minutes).
Then it seems wastefull to check every second for the os-time if this time has reached.

You could get the os-time once, do a diff for how long to wait and then create a timer for that amount.
When the timer reaches its end, then you could either do want you need to do or start the "trigger every second" if it really needs to be on the exact os-time.

see the docs here for timer

This is indeed a very reasonable thought. A "trigger every minute" scenario would seem okay, but "wasteful" is the right word for each second, especially considering that in my case the time-distance can reach several hours. Thanks for the hint, aipie!

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 Frequently asked questions and 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 [email protected] with your username.