Creating a time system

0 votes
asked Jan 16 by PsychoFox (640 points)
edited Jan 17 by PsychoFox

So I want to implement a time system in my game.

There are three elements to this system:
    1. Day of the week: Monday to Sunday
    2. Time of day: Morning and Evening
    3. A day count since beginning of the game
I will have two buttons in the game:
    1. "pass time" which advances Morning to Evening
    2. 'Sleep' which advances a day, logically advancing the day of the week.

I've already setup:

<<set $weekDays=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]>>


<<set $dayTimes=["Morning", "Evening"]>>


1. How do I implement the two buttons to do as I intend?
2. How do I display this information?
    I assume I'll need three variables,  $day, $timeOfDay, $dayCount. How do I set them up?

1 Answer

0 votes
answered Feb 9 by mazhem (1,150 points)

Do you want time to be constantly updating, or do you want time to go forward as you do actions, or only when you press the buttons? 

I personally wouldn't suggest using an array, but then again my code isn't super pretty, I like to use a bunch of if statements to determine what's going on. 

Either way I'd suggest in your storyinit or start passage, set your day count to 0. You can determine your day off your day count if you use % in the way such as:

<<set $dayCount to 0>>

/* when press button to increase day */
<<button "sleep">><<set $dayCount++>><</button>>

/*set $day++ means set $dayCount is $dayCount + 1 which means if it's 0 it'd be 0 + 1 */

<<if $dayCount % 7 is 1>><<set $day to "Monday">>
<<elseif $dayCount% 7 is 2>><<set $day to "Tuesday">>
<<elseif $dayCount% 7 is 3>><<set $day to "Wednesday">>
<<elseif $dayCount% 7 is 4>><<set $day to "Thursday">>
<<elseif $dayCount% 7 is 5>><<set $day to "Friday">>
<<elseif $dayCount% 7 is 6>><<set $day to "Saturday">>
<<else>><<set $day to "Sunday">>


commented Feb 9 by idling (10,670 points)

Array would imo be better than this. Storyinit:

<<set $day to 0>>
<<set $daytime to "Morning">>
<<set $daycount to 0>>
<<set $week to ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]>>

Widget for the passing through days:

<<widget timepass>><</nobr>>
<<set $daytime to "Morning">>
<<set $daycount++>>
<<if $day < 6>>
	<<set $day++>>
	<<set $day to 0>>

The buttons:

<<button "Rest">><<set $saytime to "Evening">><</button>>

<<button "Sleep">><<timepass>><</button>>

Using the results:

It's $week[$day] $daytime


commented Feb 9 by mazhem (1,150 points)
Arrays automatically cycle?
commented Feb 10 by idling (10,670 points)

No, that's what

	<<set $day to 0>>

  is for..

commented Feb 10 by mazhem (1,150 points)

This was the part I didn't get, that then pulls the number set from that point in the Array? 

commented Feb 10 by idling (10,670 points)

Basically, instead of having $day be a string, it is a number corresponding to one of the elements of the $week array. $week[0] is "Monday" and $week[6] is "Sunday". and all the other days can be covered by the numbers in between. So instead of writing this really long if statement you can just write:

<<if $day < 6>>
	<<set $day++>>
	<<set $day to 0>>



will print you your current day.

commented Feb 10 by mazhem (1,150 points)
Rad, ty for helping me get my head around Arrays.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.