Таймер в игре

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

Привет! Я бы хотел создать таймер в моей игре. Есть ли какой то способ сделать таймер в Label? Это можно так же использовать как время,проведенное в игре. Спасибо за все ваши подсказки!

Hey! I would like to create a timer in my game. Is there some way to make a timer in the Label? This can also be used as the time spent in the game. Thanks for all your tips!

:bust_in_silhouette: Reply From: Dayls

You can create timer by writing this code (sorry, it is c#):

Timer timer = new Timer();
AddChild(timer);
timer.Connect("timeout", this, "OnTimerTimeout");
timer.WaitTime = waitTime;
timer.OneShot = false;
timer.Start();

void OnTimerTimeout()
{
   // there should be some code to execute when the timer expires
}

For more detailed information you can look in documentation.

Okay, thanks a lot anyway. But is there really no even simple alternative to the timer in GDScript?

ZeralldGames | 2021-06-13 00:49

:bust_in_silhouette: Reply From: FoxyProgramm

В общем есть одно костыльное решение

var t : float = 0.0
func _physics_process(delta):
    t += 1.0/60.0
    print(t)

так вот _physics_process обновляется 60 раз в секунду, если прибавлять каждый
тик прибавлять 1/60, то в принципе можно сделать кривой косой, но рабочий таймер
или можно так

var t : float = 0.0
func _timer():
    print(t)
    t += 1.0
    yield(get_tree().create_timer(1.0), 'timeout') 
    #in godot 4.0 await get_tree().create_timer(1.0/60.0).timeout
    _timer()