A bit of help with my time system

Twine Version: sugarcube 2.36.1

Hello! I have racked my brain long enough on this and need some help. I have a very simple time system I picked up from some post that works by periods which almost works great for what I’m doing but the way it handles days has stopped progress all together. It will count the periods of the day then when it hits max periods it will switch to next day number then sign the number a week day name defined in storyinit but I can not find a way to make those weekday names in to a variable I can use.

/ story  init /

/% 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 ["Early Morning", "Morning", "Noon", "Afternoon", "Evening", "Night", "Asleep"]>>

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

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

/ and the widgets /

<<widget "now">>
	\It is <<print setup.DAYS[$day % 7]>>, <<print setup.PERIODS[$period]>>, Day $day.
<</widget>>
/*
 *	<<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">>
	\<<silently>>
		<<set _offset to 1>>
		<<set _periodsInDay to setup.PERIODS.length>>
		
		<<if $args.length > 0>>
			<<set _offset to $args[0]>>
		<</if>>

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


/*
 *	<<NextMorning>>
 *
 * Advances the current Time Period to the Morning of the next day.
 */
<<widget "NextMorning">>
	\<<silently>>
		/% 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 0>>
	<</silently>>\
<</widget>>

It will count the days as numbers that keeps going up, so say if I wanted to do

<<if $day gte 0 and $day lte 6>>
	<<if $period gte 0 and $period lte 5>>
		[[walk in to school|school1]]
	<</if>>
<</if>>

this works as it will not fire past the $day6 but the week doesn’t stop at day 7 it will call and print the next Monday as day 8

my question is are there anyways to make this stop at day7 and the next Monday restart to day1 then keep the day number from being printed or can I some how turn the day name in to a variable? I have tried to figure it out myself but i just started coding to make this game this month so I gave it a go but here I am.
any help would be appreciated!

You are using the correct logic in your “now” widget, where you use the % sign to get the mod of the day number. You can apply the same logic in your if statement:

<<if $day % 7 gte 1 and $day % 7 lte 6>>
	<<if $period gte 0 and $period lte 5>>
		[[walk in to school|school1]]
	<</if>>
<</if>>

This way, if it is the second week of the game and the $day is 8, for example, “8 % 7 = 1”, which is a Monday again.

Note that I change the first condition to “gte 1” because “$day % 7” = 0 means it is a Sunday, based on your “setup.DAYS” array.

2 Likes

Oh thank you it works perfectly! Here I thought I was going to have to rewrite a month worth of game events! Would of never to have thought to try the percent sign that is in the setup. I assumed it has something to do with the math it was doing to assign the day names. will mark it up as more witch craft in coding. also weirdly enough the Sunday in that is day 7 for what ever reason but it works so i cant complain lol, but thanks again!

2 Likes

Glad to help! Just so you don’t think it’s witchcraft :slight_smile: , the percent is actually the symbol that returns the rest of a division instead of the result of a division.
If you divide 7 by 7, the result is 1, but the rest is 0, because 7 is perfectly divisible by 7.
So 7 % 7 = 0.

1 Like