Getting a character to sleep

For my project, I’m using four recurring scenes–called dawn, day, dusk, night–to organize each day into time periods. This is one of the examples provided by Inform 7. Here’s the code, which may look familiar:

[spoiler]The sun is a backdrop. It is everywhere. The description is “The sun blazes in the sky.”

Night is a recurring scene. Night begins when Dusk ends. Night ends when the time since night began is 15 minutes. Night ends when Slumber ends.

When Night begins:
say “The sun sinks into the distance and the island falls into shadows.”;
now the description of the sun is “The sun is out of sight. Just like all hope of rescue.”

Dawn is a recurring scene. Dawn begins when play begins. Dawn begins when Night ends. Dawn ends when the time since Dawn began is 5 minutes [gotta fanagle with time intervals, prolly work with turns]. Dawn ends when Slumber ends.

When Dawn begins:
say “The sun rises, casting rays of gold and sapphire over the island, driving away the darkness.”;
now the description of the sun is “The sun is rising. Maybe you’ll be rescued soon.”

Day is a recurring scene. Day begins when Dawn ends. Day ends when the time since Day began is 15 minutes [fanagle, too]. Day ends when Slumber ends.

When Day begins:
say “The sun has fully risen.”

Dusk is a recurring scene. Dusk begins when Day ends. Dusk ends when the time since Dusk began is 5 minutes. Dusk ends when Slumber ends.

When Dusk begins:
say “The sun has fallen from its zenith. It begins its descent into the horizon.”[/spoiler]

However, this way of organizing time periods doesn’t seem to meet my purposes. For one, I want the player to be able to sleep through a time period. (“If it is day and he falls asleep, now he is awake and it is evening” something or another…). But I don’t know how to achieve this.

This is some of my own code:

[spoiler]Instead of sleeping:
if the player is in the sleepsack:
if the campfire is in the Beach:
say “You go to sleep…”;
now the player is asleep;
if the player is asleep:
now the player is awake;
otherwise:
say “You should light a campfire before going to bed.”;
otherwise:
say “It’s not very comfortable for sleeping here.”

A person can be awake or asleep. The player is awake.

Slumber is a scene. Slumber begins when the player is asleep. Slumber ends when the player is awake.

When Slumber ends:
say “Several hours later, you awaken.”[/spoiler]

I’m sure if I could direct the Slumber scene to begin and end when the player is either sleeping or awake, respectively, then as long as time period scenes end when Slumber ends, this would work. But I might also need a totally different approach, I just don’t know.

Your help is immensely appreciated. Sorry if I’ve posted this topic in the wrong thread.

So I’ll preface by mentioning I’m not the best when it comes to Inform and there are probably better ways to do this. But in your place, I would not make sleeping a scene and instead create a truth state that triggers the start of the appropriate scene. Additionally, the Dawn, Day, Dusk, and Night scenes are coded to always fire in that order, and you want to jump around when sleeping. You’ll need one more truth state to essentially break the sequence.

Using falling asleep at dusk and waking up at dawn as an example:

[code]The wake at dawn flag is a truth state that varies. The wake at dawn flag is false.
The sleeping flag is a truth state that varies. The sleeping flag is false.

Instead of sleeping:
if the player is in the sleep stack:
if the campfire is in the Beach:
now the sleeping flag is true;
if Dusk is happening:
now the wake at dawn flag is true;
[here is where setting the states for Night, Dawn, and Day go]
otherwise:
say “You should light a campfire before going to bed.”;
otherwise:
say "It’s not very comfortable for sleeping here.

Dusk is a recurring scene. Dusk begins when Day ends and when the sleeping flag is false. Dusk begins when the wake at dusk flag is true. Dusk ends when the time since Dusk began is 5 minutes. Dusk ends when the wake at dawn flag is true.

Dawn is a recurring scene. Dawn begins when play begins. Dawn begins when Night ends and when the sleeping flag is false. Dawn begins when the wake at dawn flag is true. Dawn ends when the time since Dawn began is 5 minutes. Dawn ends when the wake at dusk flag is true.

When Dawn begins:
say “[if the wake at dawn flag is false]The sun rises, casting rays of gold and sapphire over the island, driving away the darkness[otherwise]You close your eyes and
fall fast asleep. It’s a dreamless slumber, and you awaken to a beautiful sunrise feeling refreshed[end if].”;
if the wake at dawn flag is true:
now the wake at dawn flag is false;
the sleep flag resets in one turn from now.

At the time when the sleep flag resets:
now the sleeping flag is false.

[/code]

I haven’t tested it, though I think the only problem would occur if the player immediately went back to sleep when they woke up. You could create another rule to prevent that.

I’m getting the following error message:

[spoiler]Problem. You wrote ‘Dawn begins when Night ends and when the sleeping flag is false’ : but that’s not one of the known ends for that scene, which must be declared with something like ‘Confrontation ends happily when…’ or ‘Confrontation ends tragically when…’.

See the manual: 10.2 > 10.2. Creating a scene

Problem. You wrote ‘Dawn ends when the wake at dusk flag is true’ : but you have already told me a condition for when that happens, and although a scene can be linked to the beginning or ending of any number of other scenes, it can only have a single condition such as ‘when the player is in the Dining Car’ to trigger it from outside the scene machinery.

Problem. You wrote ‘Dusk begins when Day ends and when the sleeping flag is false’ : again, that’s not one of the known ends for that scene.

Problem. You wrote ‘Dusk ends when the wake at dawn flag is true’ : again, you have already told me a condition for when that happens.[/spoiler]

So I’m still stumped.

You probably need to write “Dawn begins when Night ends and the sleeping flag is false” without the second “when.”

For the other problem, maybe you could “or” all the separate clauses for Dawn ending together; or it might be better to write a “To decide whether it’s time to wake up” phrase that contained all those conditions and then you could say “Dawn ends when it’s time to wake up.”

Not quite there, but making progress. It worked when I had “Day begins when Dawn ends,” but because I want to skip a scene if the player gets a full 8 hours of sleep, now the general problem now is that I’m trying to get a scene to begin when another scene ends a certain way, or when another scene ends a certain way. For example, I write “Day begins when Dawn ends wakefully. Day begins when Night ends restfully.” But, of course, this is what Inform says:

Problem. You wrote ‘Day begins when Dawn ends wakefully’ : but that’s not one of the known ends for that scene, which must be declared with something like ‘Confrontation ends happily when…’ or ‘Confrontation ends tragically when…’.

Here’s my updated code, this for sleeping:

[spoiler]The wake at dawn flag is a truth state that varies. The wake at dawn flag is false.
The wake at day flag is a truth state that varies. The wake at day flag is false.
The wake at dusk flag is a truth state that varies. The wake at day flag is false.
The wake at night flag is a truth state that varies. The wake at night flag is false.
The sleeping flag is a truth state that varies. The sleeping flag is false.

Instead of sleeping:
if the player is in the sleepsack:
if the campfire is in the Beach:
now the sleeping flag is true;
say “You go to sleep…”;
if Dusk is happening:
now the wake at dawn flag is true;
if Night is happening:
now the wake at day flag is true;
if Dawn is happening:
now the wake at dusk flag is true;
if Day is happening:
now the wake at night flag is true;
otherwise:
say “You should light a campfire before going to bed.”;
otherwise:
say “It’s not very comfortable for sleeping here.”

At the time when the sleep flag resets:
now the sleeping flag is false.[/spoiler]

And the part to control the day-cycle:

[spoiler]Day is a recurring scene. Day begins when Dawn ends wakefully. Day begins when Night ends restfully. Day ends wakefully when the time since Day began is 15 minutes and the sleeping flag is false. Day ends restfully when the wake at night flag is true.

When Day begins:
now the description of the sun is “The sun blazes in the sky.”;
say “[if the wake at day flag is false]The sun has fully risen[otherwise]You close your eyes and fall fast asleep. It’s a dreamless slumber, and you sleep through the morning, waking up in the afternoon feeling refreshed[end if].”;
if the wake at day flag is true:
now the wake at day flag is false;
the sleep flag resets in one turn from now.

Dusk is a recurring scene. Dusk begins when Day ends wakefully. Dusk begins when Dawn ends restfully. Dusk ends wakefully when the time since Dusk began is 5 minutes. Dusk ends restfully when the wake at dawn flag is true.

When Dusk begins:
now the description of the sun is “The setting sun is a deep red and casts shades of purple and ochre across the sky, like a burning furnace.”;
say “[if the wake at dusk flag is false]The sun has fallen from its zenith. It begins its descent into the horizon[otherwise]You close your eyes and sleep through the day, and wake up just as the sun begins to set[end if].”;
if the wake at dusk flag is true:
now the wake at dusk flag is false;
the sleep flag resets in one turn from now.

Night is a recurring scene. Night begins when Dusk ends wakefully. Night begins when Day ends restfully. Night ends wakefully when the time since night began is 15 minutes. Night ends restfully when the wake at day flag is true.

When Night begins:
now the description of the sun is “The sun is out of sight. Just like all hope of rescue.”;
say “[if the wake at night flag is false]The sun sinks into the distance and the island falls into shadows[otherwise]You close your eyes and sleep. When you wake up, night has settled over the island[end if].”;
if the wake at night flag is true:
now the wake at night flag is false;
the sleep flag resets in one turn from now.

Dawn is a recurring scene. Dawn begins when Night ends wakefully. Dawn begins when Dusk ends restfully. Dawn ends wakefully when the time since Dawn began is 5 minutes. Dawn ends restfully when the wake at dusk flag is true.

When Dawn begins:
now the description of the sun is “The sun is rising. Maybe you’ll be rescued soon.”;
say “[if the wake at dawn flag is false]The sun rises, casting rays of gold and sapphire over the island, driving away the darkness[otherwise]You close your eyes and fall fast asleep. It’s a dreamless slumber, and you awaken to a beautiful sunrise feeling refreshed[end if].”;
if the wake at dawn flag is true:
now the wake at dawn flag is false;
the sleep flag resets in one turn from now.[/spoiler]

You’ve all been real helpful, by the way. Thanks! :slight_smile: