I think you've got multiple issues here:
- First of all, you're converting text back to a number, which is something you should avoid. Why not store the actual numbers?
- You shouldn't have to constantly check whether the time has changed or is different. You know when it changes, so I'd absolutely go with signals instead.
This this from memory, so might include mistakes, but should give you a general idea on how I'd solve this (to be put into its own standalone script, e.g.
signal highscore(best: float, previous: float)
var best: float
var first: bool = true
var inverted: bool
func reset() -> void:
first = true
func track(score: float) -> void:
if self.first or (self.inverted and self.best < score) or (self.best > score):
self.first = false
var previous = self.best
self.best = score
emit_signal("highscore", self.best, previous)
Once this is implemented, you can track scores using this new class:
func _ready() -> void:
lap_tracker = ScoreTracker.new()
lap_tracker.inverted = true
lap_tracker.connect("highscore", self, "_on_highscore")
func _on_highscore(best: float, previous: float) -> void:
# Update UI
# Whenever a lap finishes:
And yes, while this also ignores float imprecision, you shouldn't get repeated events, unless you indeed got a tad bit faster, even if it's just 1 microsecond.