# How to create a time system?

reopened
Hello, first off I'm a noob so bare with me.

In my game I plan to have a time system, in which it displays on the sidebar (sorry for not know what it is called). I'm also try to include that I can add time from certain events, also having characters following a schedule and show up at certain locations from what time it is.

What I need help with is, displaying the time on the side with a day counter and creating time itself. The items I want to display on the side is, &ldquo;How many days&rdquo; &ldquo;What day it is (Monday - Sunday)&rdquo; &ldquo;What time (Morning, Noon, Afternoon, Evening, Night)&rdquo;

How would I go about setting it up?

Thanks,

by (159k points)
selected

When you say 'time system' do you mean?

1. One that consists of only 5 time blocks (Morning, Noon, Afternoon, Evening, Night).
In which 'adding time' means moving forward from one time block to the next until you reach 'Night' at which point the next block would be 'Morning' of the next day.

eg. Day 1 Morning becomes Day 1 Noon, Day 1 Noon becomes Day 1 Afternoon, etc... until Day 1 Night becomes Day 2 Morning.

2. One that uses a 24 Hour (60 Minutes per hour) in-game clock just like in the Real World.
In which 'adding time' means adding Hour(s) and/or Minute(s) to the current game time until PM becomes AM again at which point the Day is increased by 1.

3. Some other time system.
by (150 points)
Number 1. I want it to be broken up into Morning, Noon, afternoon, night. While also keeping track of Monday, Tuesday, Wednesday, etc. The whole day counter was so I could set it up for events happening on certain days. So it would b like, "It is Monday, Afternoon, Day 57"

Hopefully this clears it up.
by (159k points)

The following shows how to setup a very basic Block based time system.

1. Place the following within your StoryInit special passage.

It defines the names of the "Days of the Week" and of the "Time Periods in a Day" on the special setup variable so that they aren't added to the story's saves. It also defines the \$day and \$period story variables which are used to track the current day and period respectively.

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

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

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

NOTE: The comments within the above (and proceeding) code examples can be safely removed, they are only there to try and add some clarity to what is happening.

2. Place the following within a widget tagged Passage.
(The name of the passage isn't important and can be whatever you like, I named mine StoryWidgets)

``````/*
*	<<now>>
*
* Displays the current Day of Week, Period of Day and Day Number.
*/
<<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.
*/
\<<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 1>>
<</silently>>\
<</widget>>
``````

...the following is a simple example of how to use the above widgets.

``````@@#now;<<now>>@@

<<replace "#now">><<now>><</replace>>

<<replace "#now">><<now>><</replace>>

<<replace "#now">><<now>><</replace>>

You will of noted that the PERIODS array defined in point 1 includes an extra period at the start of the day named "Asleep". I did this so that you can have events that occur while the Main Character is asleep (eg. Dreams, etc...), which would otherwise between the last period of a day "Night" when they go to bed and the start of the next day "Morning" when they wake up. You can obviously rename that period (or any other) to whatever makes sense to you.

by (150 points)
Thank you, I have made a similar system while I was waiting. It is almost like yours but, looking at mine I see where some mistakes are made. I'll probably end up using yours but with a different take on it.

I would message you if it was on this site but... Once again, thank you.
by (220 points)
Hello there,

I was just searching for a time system to implement in my own game and this is almost exactly what I was looking for myself!

If you don't mind me asking, how would I best go about adding a date/month/year system into this?
+1 vote
by (44.7k points)
edited by

Well, it depends on how accurate you want to go, because technically the sunrise-sunset time changes throughout the year, and also depends on your latitude.  I know this because I have a super-elaborate setup to handle this in a game I'm working on.

Yeah, it's a lot of stuff.  And that's the simplified version.  (Hopefully that all works, since I chopped out a lot of code.)

You'll have to advance time yourself by using "<<IncTime minutes>>" whenever you need to advance the time.  You can also use "<<IncGameDay>>" to increase the day by one day.  You can also set the time and date directly using the \$CurHr, \$CurMn, \$CurSc, and \$GameDay variables, but you should call "<<SetDay \$GameDay>>" if you change the \$GameDay variable.

I also tossed in the Flag and Alarm functions which you can use to keep track of event flags or to set "alarms" so you can check to see if the alarm has gone off yet.  Most of the functions have comments in front of them explaining them.

Enjoy!

EDIT: To those getting the error in <<TimeDisplay>> mentioned below, make sure you have this code in your StoryCaption passage:

``````<span id="CurTime" style="padding-left: 50px;"></span>
<<silently>><<timed 5ms>><<TimeDisplay>><</timed>><<repeat 1000ms>><<TimeDisplay>><</repeat>><</silently>>``````

If you're still getting an error, then change the TimeDisplay widget code to this:

``````/*  TimeDisplay : Display the current time and show schedule on hover.  */
/*  EXAMPLE: <<TimeDisplay>>  */
<<widget "TimeDisplay">>
<<if \$ShowTime && (\$("#CurTime").length > 0)>>
<<replace "#CurTime">>
<<print ConvertTime(\$CurHr, \$CurMn, true)>>
<</replace>>
<</if>>
<</widget>>``````

That should fix it.

by (150 points)
Thank you! I'll check it out.
by (710 points)
Hello,

I tried this code, and I am getting an error when using <<TimeDisplay>> in my story passage:

Error: <<TimeDisplay>>: error within widget contents (Error: <<replace>>: no elements matched the selector "#CurTime")

I included the \$curTime code in the StoryCaption, so I am confused as to why I get this error. Is there something else I am supposed to do to initialize the system after I follow all the pastebin instructions, and change \$ShowTime in the StoryInit to 1 ?
by (110 points)
Oof, it looks like I'm having the exact same problem too!

Sorry for bumping a (kinda sorta) older thread but can somebody help real quicks? I'm reaaally super new to Twine so I'm not super sure what to do :c