Spielerando

Day-Night Cycle

A day-night cycle is one clock for the whole world. Real seconds turn into hours, and the hour decides how bright it is and whether it is night, dawn, day or dusk. Scheduled events fire once each time the clock passes their hour, even in a big step or during sleep. The player can pause and sleep until sunrise.

Also called: Time of day, World clock, Day and night.

A plump hedgehog in a yellow oilskin coat stands on a stone jetty at the turn from night to morning: the sky is deep blue on one side and apricot gold on the other, half the village lamps are still glowing and a baker's shutter swings open.
Dabbledawn Quay, an invented harbour village game. Invented game, image generated with AI.

Paste this to your AI

Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.

Add "Day-Night Cycle" to my game.

What it should do: A world that changes with the hour feels alive: shops open and close, danger comes with the dark, and the player plans around the time. One clock drives all of it, so light, events and sleep can never disagree.

Build it exactly as this state machine. First check that it fits my game, then build it like this:

```mermaid
stateDiagram-v2
  [*] --> day
  day: Day
  dusk: Dusk
  night: Night
  dawn: Dawn
  day --> dusk: the sun goes down
  dusk --> night: sunset
  night --> dawn: sunrise or sleep
  dawn --> day: the light is full
```

Use these starting values:
- dayLength: 600 seconds
- sunrise: 6 hour
- sunset: 20 hour
- twilight: 1.5 hours

Watch out for these common mistakes:
- Events skipped by a big step: Tell your AI: fire an event when the clock passes its hour, looking at where it was and where it is now, and let sleep move the clock the same way a tick does.
- Counting the day at the wrong moment: Tell your AI: count a day at midnight only, and have sleep count it like any other pass of midnight.
- Dawn and dusk longer than the day: Tell your AI: cut the twilight to half of the daylight hours when the day is too short for both.
- A long stall: Tell your AI: limit one step to a day, so a stall moves the clock by at most 24 hours and the game carries on from there.
- The clock runs behind a menu: Tell your AI: pause the clock while a menu is open, and ignore sleep while it is paused.
- Landing exactly on an hour: Tell your AI: an event fires when its hour is after the old time and at or before the new one. That covers landing on it exactly, and an event at the starting hour waits until tomorrow.
- Light that jumps: Tell your AI: ease the light across the whole twilight instead of switching it.

Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.

Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP

What it does

A world that changes with the hour feels alive: shops open and close, danger comes with the dark, and the player plans around the time. One clock drives all of it, so light, events and sleep can never disagree.

How it runs, as a diagram

Everything it can be in, and what moves it on. Your AI builds from exactly this.

the sun goes downsunsetsunrise or sleepthe light is fullDayDuskNightDawn
How it runs, as a diagram
  • Day → Dusk: the sun goes down
  • Dusk → Night: sunset
  • Night → Dawn: sunrise or sleep
  • Dawn → Day: the light is full

Good starting values

ValueStart withWhat it changes
dayLength600 secondsReal time for a whole day. Ten minutes is long enough to get things done in daylight and short enough that the player sees dusk fall in one sitting.
sunrise6 hourThe hour dawn starts. Before it, and after sunset, it is night.
sunset20 hourThe hour night starts. A long summer evening or a short winter one is just a different number here.
twilight1.5 hoursHow long dawn and dusk each last. Long enough for the light to fade in slowly, short enough that most of the day is plain daylight.

What usually goes wrong

Events skipped by a big step

If an event only fires when the clock shows exactly its hour, a lag spike or a night's sleep jumps over it and the market never opens.

Fix: Tell your AI: fire an event when the clock passes its hour, looking at where it was and where it is now, and let sleep move the clock the same way a tick does.

Counting the day at the wrong moment

Counting a new day at sunrise makes sleeping before midnight and after midnight count differently, and a player can gain or lose a day.

Fix: Tell your AI: count a day at midnight only, and have sleep count it like any other pass of midnight.

Dawn and dusk longer than the day

A short day with long twilights makes dawn and dusk overlap, and the light jumps or goes below zero.

Fix: Tell your AI: cut the twilight to half of the daylight hours when the day is too short for both.

A long stall

After a loading screen or a window put in the background, the next step can be minutes long and carry the clock through whole days.

Fix: Tell your AI: limit one step to a day, so a stall moves the clock by at most 24 hours and the game carries on from there.

The clock runs behind a menu

If the clock keeps going while a menu is open, the player comes back to a different time of day, or a night they did not choose.

Fix: Tell your AI: pause the clock while a menu is open, and ignore sleep while it is paused.

Landing exactly on an hour

An event on the starting hour fires at once, or never, and one that the clock lands on exactly can be counted twice.

Fix: Tell your AI: an event fires when its hour is after the old time and at or before the new one. That covers landing on it exactly, and an event at the starting hour waits until tomorrow.

Light that jumps

Switching the light at the edge of dawn or dusk makes the screen blink from dark to bright.

Fix: Tell your AI: ease the light across the whole twilight instead of switching it.

How to check it works

Works well with

Common questions

how do i add a day and night cycle to my game

Keep one clock in hours and work out the light and the part of the day from it, never separately. Ten real minutes per day is a good start. Ease the light across dawn and dusk instead of switching it.

how do i make events happen at a certain time of day

Fire an event when the clock passes its hour, comparing the old time with the new one, not when the clock shows exactly that hour. Then a lag spike or a night's sleep cannot skip it. Tell your AI to fire it once per pass.

what happens to events when the player sleeps

They should still happen. Sleeping is just a big step of the clock, so move the clock the normal way and let everything scheduled in between fire in order. Count the day at midnight so sleep before or after midnight gives the same result.

what do i tell my ai to add a day and night cycle

Paste this: Add a day-night cycle. One clock in hours, 600 real seconds per day, sunrise at 6 and sunset at 20 with 1.5 hours of dawn and dusk. Work out light and the part of the day from the hour. Fire scheduled events when the clock passes their hour, even in a big step. Count a day at midnight, pause the clock in menus, and let the player sleep only at night, jumping to sunrise.

Where it comes from

We could not find a reliable origin for this one yet.

Where this mechanic comes from has not been researched yet.

Games are named only as references. Names and marks belong to their owners, and nothing here is endorsed by them.

What was checked

This is what separates the blueprint from a catalogue entry: the values on this page come from code that runs.

Your AI gets the code

Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it