Creating time specific events

Please specify version and format if asking for help, or apply optional tags above:
Twine Version: version 2.3.7
Story Format: SugarCube 2.31.1

Hi there, I am trying to figure out how to get certain people to be in a location on different days. i.e if Monday morning/ Wednesday Evening/ Friday midday. John is at the bar. if Tuesday all day Mary is a the bar. if Friday afternoon Alan is also at the bar. Here is the code I am using for my time and date system. Any help would be appreciated.


/% The names of the Days of the Week. %/
<<set setup.DAYS to ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]>>

/% The names of the Time Periods of a Day. %/
<<set setup.PERIODS to ["Asleep", "Morning", "Noon", "Afternoon", "Evening", "Night"]>>

/% The current Game Day: Monday. %/
<<set $day to 1>>

/% The current Time Period: Morning. %/
<<set $period to 1>>


 *	<<now>>
 * Displays the current Day of Week, Period of Day and Day Number.
<<widget "now">>
	\It is <<print setup.DAYS[$day % 7]>>, <<print setup.PERIODS[$period]>>, Day $day.

 *	<<AdvancePeriod [number of periods]>>
 * Advance current Time Period by a set number of periods, if no number
 * if pass to widget then current Time Period is advanced by 1 unit.
 *		<<AdvancePeriod>>		Advances time period by 1 unit.
 *		<<AdvancePeriod 1>>		Advances time period by 1 unit.
 *		<<AdvancePeriod 2>>		Advances time period by 2 units.
 * If the current day's time boundary is exceeded then the Day Number
 * will also be updated.
<<widget "AdvancePeriod">>
		<<set _offset to 1>>
		<<set _periodsInDay to setup.PERIODS.length>>
		<<if $args.length > 0>>
			<<set _offset to $args[0]>>

		<<set $period += _offset>>
		/% Update the Day Number as necessary. %/
		<<if $period >= _periodsInDay>>
			<<set $day += Math.trunc($period / _periodsInDay)>>
			<<set $period to ($period % _periodsInDay)>>

 *	<<NextMorning>>
 * Advances the current Time Period to the Morning of the next day.
<<widget "NextMorning">>
		/% Increament the Day Number by 1 unit. %/
		<<set $day += 1>>

			Set the current Time Period to the index of the
			"Morning" element of setup.PERIODS array.
		<<set $period to 1>>

Story passage:

<<if $gameday is Friday>>
	<<if $gametime is Morning>>
	John is here
	<<elseif $gametime is Afternoon>>
	Alan is here

You might want to check the “Time” section of my Twine/SugarCube sample code collection. It shows how to use the JavaScript Date object to work with times and dates in SugarCube.

As for handling multiple cases of options within a story passage, you should check out the SugarCube <<switch>> macro. That would let you write your cases like this:

	<<switch $gameday>>
		<<case "Monday">>
			<<if $gametime == "Morning">>
				John is here.<br>
		<<case "Tuesday">>
			Mary is here.<br>
		<<case "Wednesday">>
			<<if $gametime == "Evening">>
				John is here.<br>
		<<case "Thursday">>
			/* Bar is empty. */
		<<case "Friday">>
			<<if $gametime == "Noon">>
				John is here.<br>
			<<elseif $gametime == "Afternoon">>
				John and Alan are here.<br>
			/* Weekend code goes here. */

Note that the <<default>> section will be executed for any days which aren’t covered by the previous cases.

Also note that strings, like “Morning” and “Noon”, have to be within single- or double-quotes to work properly.

The <<nobr>> macro used above prevents any text and code within it from displaying any line breaks, unless you use something like the <br> HTML element to force a line break. This should prevent unnecessary blank lines from being produced by that code. See also the nobr passage tag and the Config.passages.nobr setting for ways to do that same thing at the passage or story level, respectively.

The “Thursday” and <<default>> cases above show how to add comments using the /* ... */ markup. Code and text within those comment markers won’t be visible to the player, they’ll only be visible if you’re looking at the code itself, so it’s handy for writing notes to yourself if you need to come back to that code later. Also note that any code within comment markers will not be executed either. Thus, if you have some code in a passage which you don’t want to run for now, but you might need that code back later, you can just put that code within comment markers to disable that code.

Hope that helps! :slight_smile:

1 Like