← Pixel Dreamer Studio

A countdown that never shows 0 while time is left

Show the seconds still to wait, rounded up, so 3, 2 and 1 each stay a full second and 0 only appears when the timer is done.

Show ceili(time_left), not int(time_left). int() cuts off the fraction, so 2.9 seconds left shows 2 and 0.9 seconds left shows 0. In our Godot 4.7 test of a 3 second Timer, int() went 2, 1, 0 a second each: the 3 was gone after the first frame, and 0 was on screen for the whole last second before the timer fired. roundi() and "%.0f" kept 3 for only half a second and showed 0 half a second early. Rounding up showed 3, 2 and 1 for a full second each and never 0 until the timer ended.

The code

Save it as countdown.gd and load it with const Countdown := preload("res://countdown.gd"). Every frame, set the Label's text to Countdown.text($OrderTimer.time_left), or Countdown.clock($OrderTimer.time_left) for minutes and seconds like 1:30. whole_seconds() gives the same number for checks like the last 5 seconds, so the warning starts on the same frame the 5 appears.

extends RefCounted
## Countdown text for a Timer, or any "seconds left" number: 3, 2, 1, then it ends.
## It never shows 0 while time is left, and every number stays up for a full second.
##   $TimeLabel.text = Countdown.text($OrderTimer.time_left)   # "3", "2", "1"
##   $TimeLabel.text = Countdown.clock($OrderTimer.time_left)  # "1:30" ... "0:01"
##   $Warning.emitting = not $OrderTimer.is_stopped() \
##       and Countdown.whole_seconds($OrderTimer.time_left) <= 5


## Seconds still to wait, rounded up: 2.3 s left = 3, 0.01 s left = 1, 0 = done.
static func whole_seconds(seconds_left: float) -> int:
	return maxi(ceili(seconds_left), 0)


static func text(seconds_left: float) -> String:
	return str(whole_seconds(seconds_left))


## Minutes and seconds, "1:30" or "0:05". An hour or more: "1:02:03".
static func clock(seconds_left: float) -> String:
	var s := whole_seconds(seconds_left)
	if s >= 3600:
		return "%d:%02d:%02d" % [s / 3600, (s / 60) % 60, s % 60]
	return "%d:%02d" % [s / 60, s % 60]

What we measured

A 3 second countdown, counted down by each frame's delta and ended once it went below 0, like a Timer, at 30, 60 and 144 frames a second, with the text read every frame. Then a 90 second clock at 60 fps, and one real Timer node read every frame until its timeout signal. Two runs gave the same numbers.

Why it happens

A countdown shows how many seconds you still have to wait, and a part of a second still counts as one to wait. int() cuts the fraction off, so it shows the seconds that are fully gone from the next number down: 2.9 shows 2 and 0.3 shows 0. Rounding moves the switch to the half second, so every number is still half a second early. Rounding up shows 1 for anything from 0.01 to 1.0 seconds, and time_left only reaches 0 when the timer stops, so 0 means the time really is up.

What to change

Ready-made kitchen effects: Cooking & Kitchen VFX for Godot 4 has an Order Timer Warning that loops while emitting is on, for the last seconds of an order, also as a 2D sprite sheet, plus Pot Boil, Pan Sizzle, Burnt Smoke and Pan Fire, in Stylized and Retro PSX looks.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Cooking & Kitchen VFX for Godot 4 has an Order Timer Warning that loops while emitting is on, for the last seconds of an order, plus Pot Boil, Pan Sizzle, Burnt Smoke and Pan Fire, in Stylized and Retro PSX looks.

More how-tos for Cooking & Kitchen VFX for Godot 4: Steam that turns to black smoke when the food burns · Make a cooking timer fire once, on time

More how-tos: UI and HUD