0 votes

Hi everyone,

on my quest of creating timers I need to get the first baby steps done. Is there any way how I can have something triggered on each OS-time-second?

I'm thinking of something like this:

var second

func _ready():

    var date_time = OS.get_datetime()

    second = date_time.get("second")

        print ("second")

This prints, but only once, so I wonder if there is something possible like "if second changes"...

(By the way, concerning _process(delta):
I always worry if _process(delta) is doing something "all the time", like it prints print ("xyz") on each frame unless there is no print...
It's as if the function is permanently checking if something is going on (using resources). But that's not the case, right? I'm asking because I have a clock running, updating each second, but if I add a print into the function it prints on each frame. Makes me nervous...)

Godot version 3.3
in Engine by (464 points)

2 Answers

0 votes

Best solution is probably to add Timer node and set wait_time to 1 second and just autostart and connect signal to it. When timeout() signal is emitted do something. Timer will start over again after emitting signal.

But if you want to do it in code only....(I think Timer node is better for most cases). First of all you want to do it in process, but you need to check few things if you want to print it every second and not frame.

var check_time = -1

func _process(delta):
    while OS.get_time().second > check_time:
        check_time = OS.get_time().second

Here I am checking while loop every frame and if system seconds is higher than -1 it prints system seconds and then change check_time variable to current system second. Because of that you won't print anything till system seconds go one up and when it does then while loop is once again correct and it will print again :)

And of course if you want to do anything every second just change print() to something.

Hope that helps.


by (77 points)
edited by

Thanks for your answer! Too bad that the timer node doesn't feature some "Hitting the OS-realtime-seconds".

golddotasksquestions on reddid suggested this way, which works as well:

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

What do you mean by "Hitting the OS-realtime-seconds"?

I mean doing something exactly when the OS-time switches to the next second (as opposed to switching to the next second "created by a timer-node", which not necessarily runs in sync with the OS-time).

0 votes
var second
func _process(delta):
        yield(get_tree().create_timer(1.0), "timeout")
        second += 1
by (80 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 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.