← Pixel Dreamer Studio

A time skip that always goes forward

Work out how many hours ahead the target is with fposmod, tween forward by that much, and wrap the hour and count the day as it passes midnight.

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.

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

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