Godot 4.4
Im trying to add a timer to my game but the problem is that it doesnt show the seconds on it
this is the code:
extends Node
@onready var templb: Label = %Templb
@onready var pontlb: Label = %Pontlb
@onready var timer: Timer = $"../objetos do nivel/Timer"
var points = 0
var temporizador = 0
func _ready():
timer.start()
func add_points():
points +=10
print(points)
pontlb.text="Pontos:"+str(points)
func _on_timer_timeout() -> void:
temporizador +=1
var min =(temporizador/60.0)
var sec = temporizador -min * 60
templb.text="Tempo: "+'%02d:%02d' % [min , sec]`Preformatted text`
I think your problem is here:
min will be a float, so you are doing x/60 and then doing x*60 so sec will always be 0.
A dirty way to fix this is to say
var min: int = int(temporizador/60.0)
var sec: float = temporizador -min * 60
However to be honest I am not certain what you are doing with your timer here. Did you want your timer to count up from 0 to a fixed limit? If so I would not do it this way. However I am uncertain about what you are trying to achieve.
For instance your temporizador is also an int set to 0. You then add 1 to it. I think this is your minutes so your timer must be set to a wait time of one minute too. So at the end of 1 minute why would there be any seconds left?
Anyway, the issue you have here is about mixing your floats and ints I think, but also there might be a problem with you logic depending on what you were hoping to achieve. Hope that helps.
i was trying to make a simple counter to see how much time it takes to complete the level