A Tween only sees numbers, and 6 is smaller than 22. In our Godot 4.7 test, tween_property(clock, "hour", 6.0, 2.0) at 22:00 ran the clock 16 hours backwards, through noon with the sun straight overhead, at 30, 60 and 144 fps. Tweening to 30.0 instead went the right way but ended on 30:00, with the day counter unchanged. The script below tweens forward by fposmod(6.0 - 22.0, 24.0), which is 8 hours, wraps the hour at midnight and adds the day: it ended at 6:00 on day 2 and never moved backwards.
The code
Save it as time_skip.gd and load it with const TimeSkip := preload("res://time_skip.gd"). Call TimeSkip.skip_to(clock, 6.0, 2.0) with the node that holds your hour, for example a day and night script, and await its finished signal before you fade back in. The node needs a float hour from 0 to 24. If it also has an int day, the script counts the new day, and if it has a bool paused, it holds the clock's own running during the skip and puts it back after.
extends RefCounted
## Skip the clock forward to an hour: sleep until morning, wait until night, jump for a cutscene.
## It always moves forward, through midnight when it has to, and counts the new day.
## Works with any node that has a float `hour` (0 to 24). Optional: an int `day` and a bool `paused`.
## const TimeSkip := preload("res://time_skip.gd")
## await TimeSkip.skip_to($DayNight, 6.0, 2.0).finished
## Hours forward until the clock next shows `to` (0 up to, not including, 24).
static func hours_until(from: float, to: float) -> float:
return fposmod(to - from, 24.0)
## Glide `clock.hour` forward to `to` in `seconds`. Returns the Tween, so you can await .finished.
static func skip_to(clock: Node, to: float, seconds := 2.0) -> Tween:
var start: float = clock.hour
var ahead := hours_until(start, to)
var start_day: int = clock.day if "day" in clock else 0
var was_paused: bool = clock.paused if "paused" in clock else false
if "paused" in clock:
clock.paused = true # the clock's own running would fight the skip
var tw := clock.create_tween()
var step := tw.tween_method(func(h: float) -> void: _set_time(clock, h, start_day), start, start + ahead, maxf(seconds, 0.001))
step.set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN_OUT)
tw.finished.connect(func() -> void:
if "paused" in clock:
clock.paused = was_paused)
return tw
static func _set_time(clock: Node, h: float, start_day: int) -> void:
clock.hour = fposmod(h, 24.0)
if "day" in clock:
clock.day = start_day + int(floor(h / 24.0))
What we measured
A clock at 22:00 skipped to 6:00 in 2 seconds, stepping the Tween by hand at 30, 60 and 144 frames a second. We counted the frames where the hour went down, how far the clock moved, and how high the sun got on the way (1 = straight up at noon, 0 on the horizon at 6:00). Then other start times, and a real Tween running on its own frames. Two runs gave the same numbers, apart from how many frames the real Tween got in its second.
tween_property()to 6.0: the hour went down on every frame until it stopped, 16 hours from 22:00 back through noon to 6:00, with the sun at 1.00 on the way. The day counter stayed on day 1. The same at all three frame rates.tween_property()to 30.0: forward, 8 hours, the sun never above the horizon, but the hour ended at 30.00 and the day counter stayed on day 1.- The script: forward on every frame, 8 hours, past midnight once, ended at 6.00 on day 2. The last hour before the wrap was 23.82 at 30 fps and 23.96 at 144. The clock's
pausedwas true during the skip and false again after. - 3:00 to 6:00: 3 hours, still day 1. 20:00 to 19:00: 23 hours forward, day 2. Already 6:00: it moved 0 hours and stayed on day 1.
- A real Tween from 23:00 to 1:00 in 1 second: finished at 1:00 on day 2 after 50 frames in one run and 62 in the other, with no frame backwards.
Why it happens
Tweens and lerp move a number from one value to another. 22 to 6 is a step of -16, so the clock counts down, even though on a clock face 6:00 comes 8 hours after 22:00. Adding 24 to the target fixes the direction, but the hour now goes past 24 and keeps going. fposmod(to - from, 24.0) is the distance forward around the clock face: 8 for 22:00 to 6:00, 3 for 3:00 to 6:00, 0 when it is already that time. The script tweens a running total by that much, and every frame writes the total wrapped to 0 to 24 back as the hour, with one more day for every 24 hours the total has passed.
What to change
seconds: how long the skip takes on screen. 2 is about right behind a short sleep fade.- The script eases in and out, so the sun speeds up and then slows down. For a steady sweep, change
TRANS_SINEtoTRANS_LINEAR. - Things that happen at a time, like a shop opening at 8:00, can be skipped over in one frame. Check a time range instead of one exact hour: night-only effects, even across midnight.
- The clock text during the skip: an in-game clock that never shows 7:60. The sun and sky for the hour: day and night cycle with sun and sky.
Ready-made skies: PD Toon Skies has 40 anime cel skies, a live sky shader and PDDayCycle, a day and night node whose shift_to() glides the time forward, past midnight when it has to. The free version has 3 skies and the same node.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: PD Toon Skies has 40 anime cel skies, a live sky shader and PDDayCycle, a day and night node whose shift_to() glides the time forward past midnight.
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
- Turn a time of day in hours into clock text that is always right