0 votes
by (640 points)
edited by

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"]>>

and

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

So:

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
by (1.1k 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">>
<</if>>

 

by (23.6k 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++>>
<<else>>
	<<set $day to 0>>
<</if>>
<</nobr>><</widget>>

The buttons:

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

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

Using the results:

It's $week[$day] $daytime

 

by (1.1k points)
Arrays automatically cycle?
by (23.6k points)

No, that's what

<<else>>
	<<set $day to 0>>

  is for..

by (1.1k points)
$week[$day]

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

by (23.6k 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++>>
<<else>>
	<<set $day to 0>>
<</if>>

Now

$week[$day]

will print you your current day.

by (1.1k points)
Rad, ty for helping me get my head around Arrays.
...