Timer syncing in seconds

Godot Version

4.3

Question

Hello! I made a timer for a speedrun mode in my game, but i found out on some phones the timer is FASTER than on other phones. (i think) in Unity there is something called delta time, does this also exist in godot?

Can you show your script? Godot does have delta time for functions like _process and _physics_process

Though a better way may be to get how many milliseconds since the start of the program using Time.get_ticks_msec(), capturing this “tick” when the game starts and reading the value as a difference since then.

var game_time_tick: int
func game_start() -> void:
    game_time_tick = Time.get_ticks_msec()

func _process(_delta: float) -> void:
    var elapsed := Time.get_ticks_msec() - game_time_tick
    # a good idea to do a little bit of math to get minutes
    # and seconds instead of just milliseconds like this
    $TimeLabel.text = str(elapsed) + "ms"
1 Like

I used for now theawait get_tree().create_timer(4.0).timeout code, and heres all of the timer:

extends Label
var timer
var milisec = 0
var sec = 0
var min = 0
@onready var end_time_det = $"/root/EndTimeDetector"

func _ready() -> void:
	set_process(false)
	await get_tree().create_timer(4.0).timeout
	set_process(true)
func _process(delta: float) -> void:
	await get_tree().create_timer(0.01).timeout
	milisec += 1
	if milisec == 60:
		sec +=1
		milisec = 0
		if sec == 60:
			min += 1
			sec=0
	timer = str(min)+":"+str(sec)+":"+str(ceil(milisec*1.3))
	end_time_det.get_time(min, sec, milisec)
	$".".text = timer

Notice your _process does have delta too, that counts seconds per-frame so you could use sec += delta.

Don’t await anything every frame, in addition the create_timer ends up adding more frame dependency since it’s run every frame but doesn’t carry over time like a Timer does.

To reduce this you could try this, key difference being only adding up by delta then determining minutes and miliseconds from seconds.

var sec: float = 0

func _process(delta: float) -> void:
	sec += delta

	var milisec: float = fmod(sec * 1000, 1000)
	var minutes: float = sec / 60
	var seconds: float = fmod(sec, 60)
	var timer: String = "%d:%d:%d" % [minutes, seconds, milisec]
	text = timer

	end_time_det.get_time(minutes, seconds, milisec)

But I would still recommend Time.get_ticks_msec() over this.

1 Like

Thanks, im gonna try it out now.

It Works! Thank you so much!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.