extends Label
var score = 0
# Called when the node enters the scene tree for the first time.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
self.text = str(score)
await get_tree().create_timer(1).timeout
score += 1
Hello, @jwogames! Please, use signals for update Label.text and await timer timeout. Don’t use _process function for this. Create a timer at your tree by code or manually and connect handler function to timeout signal. Try this code:
extends Label
var score: int = 0
# Connect this to timer timeout signal
func on_score_timer_timeout():
score += 1
self.text = str(score)
extends Label
var score = 0
# Called when the node enters the scene tree for the first time.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
self.text = str(score)
set_process(false)
await get_tree().create_timer(1).timeout
score += 1
set_process(true)
You can try this, it will work, you not need to create any timer.
Remember you’ll have multiples frames in one second, if you want just one point per second i recommend don’t use _process for that, instead create a Timer node and use the timeout signal to make your logic:
extends Label
var score = 0
func _ready() -> void:
var timer_node = Timer.new()
add_child(timer_node)
timer_node.timeout.connect(_on_timer_timeout)
timer_node.one_shot = false
timer_node.start(1.0)
func _on_timer_timeout() -> void:
score += 1
text = str(score)
_process() is called once per frame, so what you have actually done is shifting all frame logic back by one second and then adding score every frame thereafter.
There are many ways to do what you want to do. For example, you can add delta to a variable every _process(), and when the variable is greater than 1, reset it to zero and score += 1, like this:
var score := 0
var score_delta := 0.0
func _process(delta: float) -> void:
score_delta += delta
if score_delta >= 1.0:
score += 1
score_delta = 0
print(score)
Another way is create a Timer node instead of SceneTreeTimer node. According to the doc, SceneTreeTimer is a one-shot timer managed by the scene tree. But you want to use it every second.