Day/week looping system twine

Hello all

So I am creating a game in twine which features a day/week system. I have the basic coding but I don’t know how to loop everything together automatically.

Basically, after night is supposed to come morning. After Sunday comes monday, etc but also. If it’s monday night the next should be tuesday morning. And if week one is finished it should be week 2.

This is what I have in the storycaption
<<if $Day == 1>>Monday<><<if $Day == 2>>Tuesday<><<if $Day == 3>>Wednesday<><<if $Day == 4>>Thursday<><<if $Day == 5>>Friday<><<if $Day == 6>>Saturday<><<if $Day == 7>>Sunday<>

<<if $Period == 1>>Morning<><<if $Period == 2>>Afternoon<><<if $Period == 3>>Evening<><<if $Period == 4>>Night<>

Day: $Dag

Week: $Week

Kind regards

1 Like

I have a similar system in my WIP. Essentially I have one passage that gets called and controls whenever time passes in the story. It increments $time through four periods in the day, 0-3. If $time is gt 3, it then increments $day and resets $time to 0.

I only have one “week” in the game so the day doesn’t loop, but you could similarly loop days 0-6 to increment a $week variable, and then a $month. You just have to keep everything in order.

<<set $time++>>
<<if $time gt 3>><<set $time = 0>><<set $day++>><<endif>>
<<if $day gt 6>><<set $day = 0>><<set $week++>><<endif>>
[...]

This “clock” passage (I actually call it ‘tick’) then continues to another passage (called ‘tock’) which sets variables strings I call $timename and $dayname, brute forcing through code like


<<if $time =0>><<set $timename = 'Morning'>>
<<elseif $time =1>><<set $timename = 'Afternoon'>>
[...]
<<if $day = 1>><<set $dayname = 'Monday'>>
[...]
<<endif>>

It also then continues to a transitional display passage explaining to the player that time is passing based on what the variables have changed to: “The sun climbs from the horizon.” type of atmosphere text.

When I’m checking time I want the number so I can check things like <<if $time lt 2>> but I can use the set strings
"It is currently <<print $dayname>> <<print $timename>>."
to produce text like “It is currently Wednesday Morning.”

You want to set it up so your clock/calendar passage is the one thing you need to call to handle time passing from wherever you call it in the story, like

"Oh, you'd like me to make you a sword? Get comfy! This will take a while!" says the old blacksmith...
<<display 'tick'>>
...after many hours of furious clanging and forging, your new sword is ready.

So essentially the “tick” passage automatically advances the hour, advances the day if applicable (it could also advance week, etc…increment hunger and tiredness), sets all the new strings for the current time, then does a fade-out/fade-in “It is Wednesday Evening!” type transition and then continues the story.

(Note: I’m using AXMA Story Maker which uses a very old-school Twine-type code, your exact code will vary.)

Hi there! I have also implemented a time system in a recent project and just in case you want to use the concept I’ll post it here. I used the built in javascript Date() object and functions and it’s very handy to calculate passage of time if you plan on switching weekdays, months, etc without needing to create a variable for each weekday. You initialize a variable like this:

<<set $date to new Date()>>

That would set the $date variable to the exact hour you run the code, or you could set a specific date like this:

<<set $date to new Date(2010,2,8,8,0)>>

*note that the month and day parameters go from 0-11 and 0-30 respectively instead of 1-12 and 1-31

After that, if you want to increment the time it’s as simple as creating some constants and adding them to the $date whenever you need:

<<set $SECOND to 1000>>
<<set $MINUTE to 60 * $SECOND>> 
<<set $HOUR to 60 * $MINUTE>>
<<set $DAY to 24 * $HOUR>>
<<set $WEEK to 7 * $DAY>>

<<set $date to new Date($date.getTime() + $HOUR)>>

Finally, you can show the date on screen in whatever format you please, like showing just the date, the date + the hours and minutes or even the seconds. Here are a few examples, but if you need more info just look for the Javascript Date methods:

<<= $date.toDateString()>>

<<= $date.toLocaleTimeString(navigator.language, {
    hour: '2-digit',
    minute:'2-digit'
  })>>

Let me know if you have any doubts and I hope this is useful!