Help with resetting weekdays for day/time cycle

Twine Version: 2.6.2

I got a working day/time cycle widget thanks to StJohnLimbo, but now that I’m trying to implement an <<if>> function to reset the week day back to Sunday, it doesn’t work anymore. Here’s the full code:

/* Changes the time of day by one */
/* Use <<add-time>> multiple times to pass more time for the same passage */
<<widget "add-time">>

	<<set $gameTime += 1>>
    
    <<if setup.time[$gameTime] == "Midnight">>
    
         <<done>>
         
        	<<replace "#regular-content">>
            
            	It's getting late. You decide to head to bed before you feel more tired.
            
            	<br><br><<button "Okay.">>
                    
                    <<set $gameTime = $playerAlarm>>
                    
                    <<set $gameDay += 1>>
                    
                    <<set $monthDay += 1>>
                    
                    <<set $totalDays += 1>>
                    
                    // Resets the day to Sunday when it's Saturday at Midnight
                    <<if $gameDay == "Saturday" && $gameTime == "Midnight">>
                        
                        <<set $gameDay = 0>>
                    
                    <</if>>
                    
                	<<goto [[Disclaimer]]>>
                    
                <</button>>
                
            <</replace>>
            
         <</done>>
         
 	<</if>>
    
    /* do further checks and messages here, as necessary */
    /* (to track the possible change in weekdays and months etc.) */
    
<</widget>>

I’m thinking that the error is the <<set $gameTime>> in this section (referenced below), but I tried retyping it for spelling errors, adding two equal signs instead of one, but with everything I tried the day after Saturday at midnight the day shows as [undefined]. Everything else works fine except for that one line.

// Resets the day to Sunday when it's Saturday at Midnight
<<if $gameDay == "Saturday" && $gameTime == "Midnight">>
                        
       <<set $gameDay == 0>>
                    
<</if>>

Edit for clarity: I’m not sure if the error comes because of an updated Twine version (since I’m using my laptop rather than my desktop at the moment), but I don’t think that would cause an issue.

There are a couple of issues:

1)

$gameDay is a number, as can be seen in the line <<set $gameDay += 1>>, where the number is increased.

The system (which cnull originally posted in another thread) uses that number as an index into the array setup.dateDay. Compare the code in StoryInit:
<<set setup.dateDay = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]>>

If you want to check which weekday it is, you can either use the number directly:

<<if $gameDay == 6>>

or you can get the name of the day from the array, using the $gameDay number for the index, and compare that name:

<<if setup.dateDay[$gameDay] == "Saturday">>

The same goes for $gameTime, which is a number like $gameDay. So a comparison like this…

… will not work as intended, because the numbers aren’t equal to the textual names.

2)

When you follow the logic in the widget as posted above, you’ll see that it increases the $gameTime by 1 (<<set $gameTime += 1>>) and then checks whether the time has reached Midnight: <<if setup.time[$gameTime] == "Midnight">>. So we are at Midnight when the contents of the outer if block are executed.

Then, a few lines down, you have “<<set $gameTime = $playerAlarm>>”, which resets the current $gameTime to the player’s alarm clock time, which is set to 1 in StoryInit in the system from the previous thread: <<set $playerAlarm = 1>>.

So at that point (and if the player hasn’t changed the alarm clock), the $gameTime is 1, which corresponds to the second entry (“Morning”) in the setup.time array.

But then, three lines down, you check whether the $gameTime is Midnight, with <<if $gameDay == "Saturday" && $gameTime == "Midnight">>. For reasons explained above, that won’t work this way, since you generally need to compare either numbers to numbers or strings to strings. But regardless of that aspect, the second problem here is that the $gameTime was at that point already reset to 1 (corresponding to “Morning” in the array), so it can’t be Midnight.

In addition to that, we don’t need to check for Midnight here, because we already know that we are inside the outer if block which only executes when it is Midnight.

So I’d probably just check whether it’s Saturday, because then we already know that we are at the roll-over to Sunday, and need to reset the weekday ($gameDay) to 0 to make it Sunday now.
If it’s not Saturday, we can just increase the $gameDay by 1 as usual. (Generally speaking, we could alternatively use modulo arithmetic in various places, but I think it’s clearer if we do it this way.)

So, the code for the widget could look like this, for example (I tested this briefly, and it worked, i.e. one could go through more than a week, with correct roll-over of the weekday):

<<widget "increase-time">>
	<<set $gameTime += 1>>
    <<if setup.time[$gameTime] == "Midnight">>
        <<if setup.dateDay[$gameDay] == "Saturday">>
        	<<set $gameDay = 0>>
        <<else>>   
        	<<set $gameDay += 1>>
        <</if>>
        <<set $gameTime = $playerAlarm>>
        <<done>>
        	<<replace "#regular-content">>It's getting late. You decide to head to bed before you feel more tired.<</replace>>
		<</done>>
 	<</if>>
<</widget>> 
1 Like

I see this makes a lot of sense now.

Ahh I see. So this part gets fixed when I move it below my <<if>> statement to reset the weekday.

That makes sense, to avoid pointless repetition.

My variation was much more lazy, I just moved everything from when the button started to where the new <<if>> statement started down to below the new <<if>> statement.

/* Changes the time of day by one */
/* Use <<add-time>> multiple times to pass more time for the same passage */
<<widget "add-time">>

	<<set $gameTime += 1>>
    
    <<if setup.time[$gameTime] == "Midnight">>
    
         <<done>>
         
        	<<replace "#regular-content">>
            
            	It's getting late. You decide to head to bed before you feel more tired.
            
            	<br><br><<button "Okay.">>
                    
                    // Resets the day to Sunday when it's Saturday at Midnight
					<<if setup.dateDay[$gameDay] == "Saturday">>
                        
       					<<set $gameDay = 0>>
                    
					<</if>>
                    
                    <<set $gameTime = $playerAlarm>>
                    
                    <<set $gameDay += 1>>
                    
                    <<set $monthDay += 1>>
                    
                    <<set $totalDays += 1>>
                    
                	<<goto [[Disclaimer]]>>
                    
                <</button>>
                
            <</replace>>
            
         <</done>>
         
 	<</if>>
    
    /* do further checks and messages here, as necessary */
    /* (to track the possible change in weekdays and months etc.) */
    
<</widget>>

Thanks a bunch for the help again :slight_smile:

1 Like

Have you tested that your variant is working as intended? Because it seems that you’re skipping over Sunday directly to Monday now. If it’s midnight on Saturday, you set the $gameDay to 0 (corresponding to Sunday), which is right. But then, two lines after the end of the if block, you have

<<set $gameDay += 1>>

which will increase the $gameDay by 1 no matter which day it is. So, if it was midnight on Saturday and the $gameDay has just been set to 0 (= Sunday), then this line will directly set it to 1 (= Monday), so Sunday will be skipped.

I’d suggest putting the <<set $gameDay += 1>> in an else block, as in my example above (repeated below), because you only want to do one or the other on any given day roll-over.

<<if setup.dateDay[$gameDay] == "Saturday">>
    <<set $gameDay = 0>>
<<else>>   
    <<set $gameDay += 1>>
<</if>>
1 Like

Oh yeah, good catch. I didn’t notice that when I was testing it. Guess it’s far better to be thorough than to try to be lazy. Thanks again.

Just a heads up though, I’m currently working on the month roll-overs (with February leap year shenanigans) and I might come crawling back soon…

1 Like