+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))
asked Feb 15, 2017 in Engine by Bishop (450 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
answered Feb 18, 2017 by lukas (679 points)
selected May 19, 2017 by Bishop

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 How to use this Q&A? before posting your first questions.