0 votes

Привет! Я бы хотел создать таймер в моей игре. Есть ли какой то способ сделать таймер в 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!

Godot version 3.3.1
in Engine by (49 points)

2 Answers

0 votes

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.

by (38 points)

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

0 votes

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

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

так вот physicsprocess обновляется 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()
by (14 points)
edited by
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.