Random beginner-question: trigger on each OS-time second

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

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")

	if(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…)

:bust_in_silhouette: Reply From: sporx13

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:
		print(OS.get_time().second)
		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 :slight_smile:

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

Hope that helps.

https://i.imgur.com/gkzNHry.gif

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
	print(time.second)

pferft | 2021-05-19 19:03

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

Eric De Sedas | 2021-05-20 03:12

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).

pferft | 2021-05-20 11:16

:bust_in_silhouette: Reply From: TTF DPC
var second
func _process(delta):
        yield(get_tree().create_timer(1.0), "timeout")
        second += 1