+15 votes

Hi
I want to use timer node for attack function, and other things later, but
I have zero idea how to use it, and information online didnt help at all.

How can i write code, that it starts counting time, for example, from when i press X button, and then when it has reached, 2 second, only then X button becomes active again.. or suggest your way, plz?

Godot info page bellow, contains info, but its so dull and has no examples.. : / http://docs.godotengine.org/en/stable/classes/class_timer.html

in Engine by (225 points)

6 Answers

+7 votes

The class reference does not have examples of use, a page with examples for each class could be good to have (but not on reference), anyway...


Somewhere, like in _ready:

timer = Timer.new()
timer.connect("timeout",self,"_on_timer_timeout") 
#timeout is what says in docs, in signals
#self is who respond to the callback
#_on_timer_timeout is the callback, can have any name
add_child(timer) #to process
timer.start() #to start

Then, on the connected node (self in this case, could be the parent or any other node in the scene)

func _on_timer_timeout():
   your_timer_stuff()
by (7,894 points)

thanks for this explanation. i assume that the last line should be timer.start(delay), with delay a float to specify after how many seconds the timer should trigger?

i just spent quite some time being confused why the timer did not work until i realized that for the start call to actually do anything, the node where you add the timer to needs to already be in the scene tree. i was instantiating a node, setting up a timer inside that node and then added the node into the tree. does not work that way (imho start() should trigger an error in this case), but does work when doing it the other way round, first adding the node, and only then starting the timer.

+4 votes

As your request... I'm using a gui button instead of keystroke but the example is still valid.

enter image description here

extends Node2D

var timer
var button

func _ready():

    timer = get_node("Timer")
    button = get_node("Button")

    timer.set_wait_time( 2 )
    button.connect("pressed", self, "_on_Button_pressed")
    timer.connect("timeout", self, "_on_Timer_timeout")

func _on_Button_pressed():
    timer.start()
    button.set_disabled(true)

func _on_Timer_timeout():
    button.set_disabled(false)
by (1,469 points)
edited by
+5 votes

Here is an in-depth 5 minute long video response to your question.

Demonstrating how you can create and use a Timer to make a forced delay between shooting a cannon ball.
QA Answer

by (491 points)
0 votes

Actually in code, if you want, you don't have to use the Timer node Godot provides. Here's an alternative way.
A simple timer:

var timer = 0
func _process(delta):
    timer += delta

When timer reached certain time, you can simply add an if statement. Or using signal for better control:

var timer = 0
var wait_time = 1
signal timeout
func _on_timeout():
    #Your code
func _process(delta):
    timer += delta
    if timer > wait_time:
        timer = 0
        emit_signal("timeout")

For more control, create more variables like disabled (Like when the timer hasn't started) and code (Anything you want)

Why the way adding delta on timer works:
delta means the time passed since the last call of _process().
So each time _process() is being called, the timer will be updated.

by (811 points)
edited by
0 votes

onready var name = $"Timer"

func _ready():
timer.start()

func onTimer_timeout():
print("test")

by (255 points)
0 votes

Hey.
You can try using yield(gettree().createtimer(2), "timeout")

by (14 points)
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.
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 webmaster@godotengine.org with your username.