Wednesday, March 20, 2019

10. Timer

Under a Node, we have 2 childs: Label and Timer.


We attach a script to Node, which operates at a rate of 0.2 Hz (1/5 sec), that is, emits a 'timeout' signal every 1/5 second.



extends Node

var label
var timer
var count = 0

func _ready():
    label = get_node("Label")
    timer = get_node("Timer")
    timer.connect("timeout",self,"_on_Timer_timeout")
    label.set_text("Count: " + str(count))
    label.set_align(Label.ALIGN_CENTER)
    label.set_valign(Label.VALIGN_CENTER)
    label.rect_scale = Vector2(2,2)
    label.margin_bottom = 100
    label.margin_right = 100
    timer.wait_time = 5
    timer.start()
 
func _on_Timer_timeout():
    count += 1
    label.set_text("Count: " + str(count))

The output, with window size of 200,200, when the count has counted upto 5:


No comments:

Post a Comment