+1 vote

Hello,
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():
    set_process(true)
    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]
    game_time.set_text(str(str_elapsed))
in Engine by (454 points)

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

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.

1 Answer

+1 vote
Best answer

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
by (675 points)
selected by

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.

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

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.