Day night cycle

How can i implement a day night cycle in a game.

Please give examples

Day is a recurring scene.
Day begins when the entire game begins.
Day ends when the time of day is 6:00 pm.
Night is a recurring scene.
Night begins when day ends.
Night ends when the time of day is 6:00 am.
Day begins when night ends.

Thanks, How and where in the code will you see the actual cycle when it is night or day


That code just implements a cycle, but it’s completely invisible to the player unless you make some additional effort.

For example, you can change the text of a room based on the time of day:

The description of the field is "[if day is happening]The sun shines down on this open field.[else]This open field is barely lit by the meagre light provided by the moon.[end if]".

Or announce to the player when the time of day changes:

When day begins:
	say "A new day has begun!".

When night begins:
	say "It's getting late!".

Or let the player know the current time of day at all times:

When play begins:
	now the right hand status line is "[if day is happening]Daytime[else]Nighttime[end if]".

My code also assumes the time of day only advances one minute at a time (which is the default). If you mess around with the passage of time, it’s up to you to think about how that affects day and night!

(You could, for example, have night start when the time is between 6:00 and 7:00, if time sometimes advances by more than a minute but never by more than an hour. But the details depend on how exactly you’re messing with time, so there’s no general-purpose answer.)


I’d say the general purpose answer would be something like Day ends when the time of day is after 6:00 pm or the time of day is before 6:00 am.

Though that technically opens up the possibility of day lasting more than a day (if time advances by, say, 20 hours), so I guess if that’s an issue you’d have to think of something else.

(Personally I’d probably just accept that night can occasionally be skipped. But your mileage may vary. In any case, it’s the approach I used to define times of day.)

1 Like

One thing to flag of potential interest is that Inform considers days to end at 4 AM; the idea is that that way 1 AM is after 11 pm (and vice versa), which is helpful for games that take place late at night but sort of odd to the extent that 5 AM is before 3 AM (docs link).

If you’re just using the scene approach, it won’t come up, but it’s worth bearing in mind if you’re comparing specific clock times to each other.


…but you only get that idiosyncratic behavior with the is before and is after phrases.

Any of >, is greater than, <, is less than perform the ordinary, expected comparison and consider 5:00 AM > 3:00 AM.