← Pixel Dreamer Studio

Night-only effects when the night crosses midnight

Check late evening OR early morning, and work out the fade with fposmod.

When the night runs from 19:00 to 5:00, check hour >= 19 or hour < 5, and work out the fade with fposmod(hour - 19, 24) so that 1:00 counts as 6 hours after the start. In our Godot 4.7 test, hour > 19 and hour < 5 was true at 0 of 48 half-hours. A fade worked out from hour - 19 was 0 from midnight to 4:30, so the fireflies vanished at midnight. The script below kept them on from 19:30 to 4:30, fading in and out over an hour.

The code

Add this node to your level, put your firefly or lantern emitters in fx, and call set_hour() from your game clock. Calling it every frame is fine. During the fade it lowers amount_ratio, so dusk has a few fireflies and the middle of the night has all of them.

extends Node
## Turns night-only effects (fireflies, lantern glow, Zzz over a sleeping villager) on and off by the clock.
## The night can cross midnight, for example from 19:00 to 5:00.
## Put your emitters in fx and call set_hour(hour) from your game clock.

@export var fx: Array[GPUParticles3D] = []
@export var night_start := 19.0  ## the hour the effects come on
@export var night_end := 5.0  ## the hour they are gone
@export var fade_hours := 1.0  ## fade in after night_start and out before night_end


func is_night(h: float) -> bool:
	if night_start < night_end:  # a range inside one day, like 13:00 to 17:00
		return h >= night_start and h < night_end
	return h >= night_start or h < night_end  # crosses midnight: late evening OR early morning


func strength(h: float) -> float:
	if not is_night(h):
		return 0.0
	if fade_hours <= 0.0:
		return 1.0
	var since_start := fposmod(h - night_start, 24.0)  # 1:00 is 6 hours after 19:00, not -18
	var until_end := fposmod(night_end - h, 24.0)
	return clampf(minf(since_start, until_end) / fade_hours, 0.0, 1.0)


func set_hour(h: float) -> void:
	var s := strength(h)
	for p in fx:
		p.emitting = s > 0.0
		p.amount_ratio = s  # a few fireflies at dusk, all of them in the middle of the night

What we measured

Night from 19:00 to 5:00 with a 1-hour fade. We checked every half-hour of the day, then drove a real emitter of 200 particles and counted lit pixels, averaged over 10 frames. Two runs gave the same results, except the pixel counts, which moved a little.

Why and never works across midnight

No hour is both after 19 and before 5 on the same clock. A range that wraps past midnight is two pieces, late evening and early morning, so the test needs or. The fade has the same problem: at 1:00, hour - 19 is -18. fposmod wraps it to 6, the real time since the night began.

What to change

A sun and sky on the same clock: day and night cycle.

Ready-made cozy effects: Cozy VFX for Godot 4 has 29 presets for farming and life games, including warm and mint fireflies, sleeping Zzz, hearts, music notes, steam from a cup or a pot, chimney smoke and falling leaves.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Cozy VFX for Godot 4 has 29 ready-made effects for farming and life games, including fireflies, sleeping Zzz, hearts, music notes, steam and falling leaves.

More how-tos for Cozy VFX for Godot 4: Turn a day counter into season, day and year without a day 0 · A JSON save that loads back exactly what you saved

More how-tos: Skies and backgrounds