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.
hour > 19 and hour < 5: on at 0 of 48 half-hours. The script: 20 of 48.- A fade from
hour - 19, clamped to 0 to 1: 0.50 at 19:30 and 1.00 from 20:00 to 23:30, then 0.00 at 0:00, 1:00, 3:00 and 4:00. - The script: 0.50 at 19:30, 1.00 from 20:00 to 4:00, 0.50 at 4:30, and 0 at 5:00 and at noon.
- The emitter at 2:00: on,
amount_ratio1.00, 1025 and 1033 lit pixels. At 19:30: 0.50, with 533 and 595. At noon: off, 0. At 0:30: on again, 1.00, with 1039 and 1015. - A range inside one day, 13:00 to 17:00: on at 15:00 and off at 20:00.
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
night_start,night_end: any hours, including a range inside one day.fade_hours: 0 switches the effects on and off at once.- An effect made of several emitters, such as fireflies with a glow layer, has more than one GPUParticles3D inside. Add each of them to
fx.
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
- 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
- Parallax2D background with no gaps
- Turn a time of day in hours into clock text that is always right
- A time skip that always goes forward