A day-night cycle usually keeps the time as hours, like 7.5 for half past seven. int((hours - int(hours)) * 60) looks right, but 1/60 is not exact in a float, so 1:01 comes out as 0.99999 of a minute and shows 1:00. In our Godot 4.7 test that happened for 623 of the 1,440 minutes of a day. roundi() instead shows 7:60 for the last half minute of every hour. Round to whole seconds first: every minute right, never :60.
The code
Set your clock Label's text to clock_text(time_of_day), where the time of day is in hours from 0 to 24, the same number that drives the sun. Times past 24 wrap around to the next day.
extends Node
## Clock text for an in-game time of day kept in hours (0 to 24), as a day-night cycle uses.
## Never shows 7:60, and never skips or repeats a minute.
static func clock_text(hours: float) -> String:
var secs := roundi(fposmod(hours, 24.0) * 3600.0) # round to the nearest second first
var mins := (secs / 60) % (24 * 60)
return "%02d:%02d" % [mins / 60, mins % 60]
What we measured
Every minute of the day as hours = minute / 60, and a running day at 60 fps where 1 real second is 1 game minute (86,400 frames). Two runs with the same numbers.
int()on the fraction: 623 of 1,440 minutes shown wrong, the first one 01:01 shown as 01:00. Running, it never showed :60 and showed all 1,440 times, but a clock set to an exact minute could show the one before.roundi()on the fraction: every exact minute right, but running it showed a time ending in :60 on 720 frames, 24 made-up times like 07:60, 1,464 different times in a day of 1,440 minutes.- The script: 0 wrong minutes, never :60, exactly 1,440 different times.
Why it happens
A float stores 1/60 as a number very slightly off. Multiply it back by 60 and you can land on 0.9999999 instead of 1, and int() turns that into 0. roundi() fixes that, but it also rounds 59.6 minutes up to 60 without moving the hour. Rounding the whole time to seconds first takes away the tiny error, and then whole-number division and % give hours and minutes that always add up.
What to change
- For a 12-hour clock, use the hour % 12, with 12 instead of 0, and add AM or PM.
- To show seconds too, use secs % 60 the same way.
The sky and sun for that time: day-night cycle with sun and sky. Effects only at night, even across midnight: night-only effects. Sleeping until morning without the clock running backwards: a time skip past midnight.
Ready-made skies: PD Toon Skies has 40 painted skies and a live sky shader with a 0 to 24 time of day slider.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: PD Toon Skies has 40 painted skies and a live sky shader with a 0 to 24 time of day slider.
More how-tos: Skies and backgrounds
- Fix a Parallax2D background that slides the wrong way, or stays still, in a CanvasLayer
- Get the direction to the sun from a DirectionalLight3D, and fade sun effects out at sunset
- Day and night cycle: sun and sky together
- Night-only effects when the night crosses midnight
- Parallax2D background with no gaps
- A time skip that always goes forward