How to allocate sleeping hours

Twine Version: 2.3.16
Story Format: 2.36.1

I want to have a mechanic where once my character gets to a place they can rest, they can type in the numbers of hours they want to sleep for, then time passes and they go about their business.

Currently, I simply have 8 hours pass in the same location, but this won’t work in all situations.

<<button "Sleep">><<minutes_incr 480>><<goto "Room 23">><</button>> (8 hrs)

I tried amending an existing bank deposit/withdrawal code which pbparjeter helped me with…

Make a deposit/withdraw.
<<textbox "$change" "Enter amount">>
<<link "Withdraw">>
<<if $change lte $Bank>>
	<<set $change to Math.trunc($change)>>
	<<set $Gold += $change>>
	<<set $Bank -= $change>>
	<<replace "#sidebar_gold">><<print $Gold>><</replace>>
	<<replace "#sidebar_bank">><<print $Bank>><</replace>>
<<endif>>
<</link>>
<<link "Deposit">>
<<if $change lte $Gold>>
	<<set $change to Math.trunc($change)>>
	<<set $Gold -= $change>>
	<<set $Bank += $change>>
	<<replace "#sidebar_gold">><<print $Gold>><</replace>>
	<<replace "#sidebar_bank">><<print $Bank>><</replace>>
<<endif>>
<</link>>

It works perfectly for banking, so I changed what looked like might be the appropriate bits of code…

Get some sleep.
<<textbox "$sleep" "How many hours?">>
<<link "Sleep">>
	<<set $sleep to Math.trunc($sleep)>>
	<<set $minutes_incr += $sleep>>
<</link>>

…but it went about as well as you would expect, given I don’t know what I am doing :slight_smile: It didn’t throw up any errors, it simply did nothing.

In my StoryInit, I have this (also from pbparjeter):

<<widget "minutes_incr">>
<<set $CurDate_inc to new Date(Math.trunc($CurDate) + $args[0] * 60 * 1000)>> <<set $CurDate to $CurDate_inc>>
<</widget>>

<<widget "check_date">>
<<set $CheckDateA to new Date($args[0])>>
<<set $CheckDateB to new Date($args[1])>>
<<if Math.trunc($CurDate) gte Math.trunc($CheckDateA) and Math.trunc($CurDate) lt Math.trunc($CheckDateB)>>
<<print $args[2]>>
<<endif>>
<</widget>>

In my StoryCaption, I have this:

Date: <<print $CurDate>>

And my button links look like this:

<<button "Go out">><<minutes_incr 30>><<goto "Town Centre">><</button>> (30 min)

This all works for the passage of time, so I was trying to tap into that.

Later, I will want to tie this to a fatigue variable, but one step at a time.

I feel you’re here trying to do the most complicated thing.
Rather than asking the player how many time to sleep, you might considere to ask when to wake up.
For instance (given $day, though I’ll of course let you use whatever variable you wish to keep track to current day):

Get some sleep. When do you wish to wake up?
<<listbox "_sleep">>
	<<option "6am" 360>>
	<<option "6:15am" 375>>
	<<option "6:30am" 390>>
	<<option "6:45am" 405>>
	<<option "7am" 420>>
	<<option "7:15am" 435>>
	<<option "7:30am" 450>>
	<<option "7:45am" 465>>
	<<option "8am" 480>>
	<<option "8:15am" 495>>
	<<option "8:30am" 510>>
	<<option "8:45am" 525>>
	<<option "9am" 540>>
<</listbox>>

<<link "You slip under your sheets and close your eyes." "Wherever the story needs to go">><<set $day +=1>><<set $hours to _sleep>><</link>>

In this case you can easily display hours anywhere with:

Current time is: <<print Math.trunc($hours/60)>>h<<if $hours%60 !=0>><<print $hours%60>><</if>>.

Will that mean rejigging my existing time layout? I don’t know enough to merge these together, if that is what is required.

In my StoryInit :

<<widget "minutes_incr">>
<<set $CurDate_inc to new Date(Math.trunc($CurDate) + $args[0] * 60 * 1000)>> <<set $CurDate to $CurDate_inc>>
<</widget>>

<<widget "check_date">>
<<set $CheckDateA to new Date($args[0])>>
<<set $CheckDateB to new Date($args[1])>>
<<if Math.trunc($CurDate) gte Math.trunc($CheckDateA) and Math.trunc($CurDate) lt Math.trunc($CheckDateB)>>
<<print $args[2]>>
<<endif>>
<</widget>>

In my StoryCaption:

Date: <<print $CurDate>>

I’m not sure I understand how your current date and hours system works right now, so my idea may have not been proper. Especially the * 1000 part, because in my book the only time a thousandth part of time has been tried was during the Révolution (10 hours of 100 minutes each), and, well, it didn’t last.

At this point I’m not able to help you, maybe I should let @pbparjeter step in!

I apologize for the inconvenience.

It’s no inconvenience. It’s likely that you and @pbparjeter have given me two equally valid methods, that simply may not work together.

I know I am diving in the deep end for someone with my limited experience, so I do appreciate your time.

As @souppilouliouma noted, a text box isn’t the best way to do this. You would need need to reject everything that isn’t a number, reject big numbers (don’t want the player to sleep for an entire year!), and convert hours to minutes and back again.

There are ways to do this with the built-in logic, but it’s not necessary.

A listbox saves some space, but it isn’t ideal given the way we set up the widget. Plain links are easier. So in the passage where players get the chance to sleep:


How long do you want to sleep for?

<<link "One hour">>
	<<minutes_incr 60>>
	<<goto "Room 23">>
	<<set $sleep_words to "one hour">>
<</link>>

<<link "Two hours">>
	<<minutes_incr 120>>
	<<goto "Room 23">>
	<<set $sleep_words to "two hours">>
<</link>>

<<link "Three hours">>
	<<minutes_incr 180>>
	<<goto "Room 23">>
	<<set $sleep_words to "three hours">>
<</link>>

<<link "Four hours">>
	<<minutes_incr 240>>
	<<goto "Room 23">>
	<<set $sleep_words to "four hours">>
<</link>>

<<link "Five hours">>
	<<minutes_incr 300>>
	<<goto "Room 23">>
	<<set $sleep_words to "five hours">>
<</link>>

<<link "Six hours">>
	<<minutes_incr 360>>
	<<goto "Room 23">>
	<<set $sleep_words to "six hours">>
<</link>>

<<link "Seven hours">>
	<<minutes_incr 420>>
	<<goto "Room 23">>
	<<set $sleep_words to "seven hours">>
<</link>>

<<link "Eight hours">>
	<<minutes_incr 480>>
	<<goto "Room 23">>
	<<set $sleep_words to "eight hours">>
<</link>>

In Room 23 (or whatever room) you can also say

You slept for <<print $sleep_words>>.

And at the bottom of Room 23 (make sure this is after <<print>>) reset it with:

<<set $sleep_words to null>>
1 Like

Perfect. Thank you once more.

1 Like

Absolutely.