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.
str(int(time_left)): 3 only right afterstart(), then 2, 1 and 0 for 1.00 s each at all three frame rates (0.99 s for the 2 at 144 fps). For the whole last second the player reads 0 while the timer is still running.str(roundi(time_left))and"%.0f" % time_left: 3 for 0.50 s (0.49 s at 144 fps), 2 and 1 for 1.00 s each, and 0 for the last 0.50 s before the end, the same at all three frame rates. Both round halves up:roundi(2.5)is 3 and"%.0f" % 2.5is 3.Countdown.text(): 3, 2 and 1 for 1.00 s each (0.99 s for the 3 at 144 fps, where the frames do not add up to exactly one second), and 0 on screen for 0.00 s before the end.- A 90 second clock with
int()and"%d:%02d": 1:29 on the first frame, 0:00 for the last 1.00 s.Countdown.clock(): 1:30 on the first frame, 0:01 on the last, 90 different texts each up for 1.00 s. - A real Timer with
wait_time = 3.0:int()showed 3, 2, 1, 0, with 0 on screen for the last 1.00 s of the run. The script showed 3, 2, 1 and 0 on no frame beforetimeout. After it,time_leftwas 0.000 and the timer was stopped.
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
- A warning for the last 5 seconds: turn it on while
Countdown.whole_seconds(time_left) <= 5and the timer is not stopped, so it ends when the order runs out. - Counting up, like a stopwatch or a race time, is the other way round: show
int(elapsed), because 0.4 seconds in, no whole second has passed yet. - Hours:
clock()switches to 1:02:05 style from one hour up. - A timer that should stop during pause: cooldown counting while paused. Firing once when the food is done: a cooking timer that fires on time. A time of day as clock text: an in-game clock that never shows 7:60.
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
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Show a percent that never says 0% or 100% too early
- Type dialogue at the same letters per second on every line and every frame rate
- Place brush dots at the same spacing however fast the mouse moves
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A coin counter that rolls up and lands on the real total