+12 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

asked Nov 8, 2016 in Engine by Serenade (222 points)

4 Answers

+4 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()
answered Nov 8, 2016 by eons (7,764 points)
+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)
answered Nov 8, 2016 by jospic (1,444 points)
edited Nov 8, 2016 by jospic
+4 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

answered Nov 8, 2016 by Tybobobo (452 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.

answered Feb 9 by Sween123 (758 points)
edited Feb 9 by Sween123
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.