How start/reset time in game( OS.get_unix_time() ) ,when the player clicks on the buttons?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bishop
:warning: Old Version Published before Godot 3 was released.

I need help with the time counting for my game
it is this:
time start at 00:00(game screen enter) - when player click on the Play button - game/time starting ( measured time spent playing level )
and when player ends the game(end game event) time stops and best time is stored in the Best time section
I have a reset button for time - when the player clicks on the Reset button time is set
to 00:00 too - this is a game reset button.
One thing I need in start time is when the player click on the Play button time start but
when it’s time for example 00:30 time is automatically set 00:00 again and starts

Thanks all for help

I have this script

extends Control

var play = false
var time_start = 0
var time_now = 0
var game_time
var button_reset
var button_play
var best_time

func _ready():
	time_start = OS.get_unix_time()
	game_time = get_node("time")
	button_reset = get_node("reset")
	button_play = get_node("play")
	best_time = get_node("best_time")

func _process(delta):
    time_now = OS.get_unix_time()
    var elapsed = time_now - time_start
    var minutes = elapsed / 60
    var seconds = elapsed % 60
    var str_elapsed = "%02d : %02d" % [minutes, seconds]

How about doing this in _process()?

if elapsed >= 30:
    elapsed = 0

It’s not complete code, just to give an idea. I’m not sure I understand what you really want though.
Note you could also use a Timer to count time instead of using get_unix_time().

Zylann | 2017-02-16 02:32

I think a fixed process timer will be more “fair” for the player than system time.

But you could make a global node to process OS time every frame, will be as imperfect as an idle timer.

eons | 2017-02-16 15:05

:bust_in_silhouette: Reply From: lukas

I would recommend to use more simple approach which is also less CPU intensive than calling OS.get_time() every frame.

var elapsed = 0.0
func _process(delta):
    elapsed += delta
    if elapsed >= 30.0:
        elapsed = 0.0

Thank you very much,
I have a few pictures/symbols and this symbols are visible at a certain time and then disappears…at the moment the time running for player and when player finish a game time stops and this is your best time.
…so it’s better to use Timer node…I see.

Bishop | 2017-02-18 20:17

You don’t need to use Timer node. Just add delta to elapsed variable in _process function for player or game.

lukas | 2017-02-19 08:57