Best (easiest) method for time and date usage

Twine Version: 2.3.16
Story Format: 2.36.1

My question is twofold. 1. What is the best method (for an inexperienced Twiner) to show the time and date for in-game playing, and 2. How best to have time pass when navigating to parts of the game, either via clicking on a map for by clicking a passage button/link?

My code so far is thus:

In StoryInit I have this:

<<set $CurDate to new Date('August 19, 1575 23:15:30')>>

I can’t remember where I saw this code, but it seems to work.

In StoryCaption, I have this:

Date: <<print $CurDate>>

In StoryStart, I have this:

<<nobr>><<set addMinutes(90)>><</nobr>>[[I need to find the hotel where my parents are staying.|Hotel]]

My initial plan was to have the above code for all links, so I could control how much time has passes from the current location to the linked location, but it seems like a long winded way of doing it. Also, if I later need the change the period of time from one location to the next, I would obviously need to revisit every link to change it as well, so it seems like there must be a better way to do this. However, if the above is actually the best method for me, have I added it to the button/link correctly?

I eventually want to have events happen at certain times, maybe also at certain dates, but I also want to have some events unavailable due to the time of day. But I have the sneaking suspicion that I am setting myself up for failure later if I do it the way I have outlined above.

I have some small HTML5 experience, but less Twine experience, so if you could include the specific passage names I may need to adjust/add/remove, please make it clear. If there is a way to misinterpret an answer, I will find it. :slight_smile:

Thank you in advance.

Taking it from the top:

In your StoryInit passage

<<set $CurDate to new Date('August 19, 1575 23:15:30')>>

And in your StoryCaption

Date: <<print $CurDate>>

In a passage tagged widget with any title, insert this. We’re creating a widget that will increment the time by a variable and another widget that will check the time

<<widget "minutes_incr">>
<<set $CurDate_inc to new Date(Math.trunc($CurDate) + $args[0] * 60 * 1000)>> <<set $CurDate to $CurDate_inc>>

<<widget "check_date">>
<<set $CheckDateA to new Date($args[0])>>
<<set $CheckDateB to new Date($args[1])>>
<<if Math.trunc($CurDate) gte Math.trunc($CheckDateA) and Math.trunc($CurDate) lt Math.trunc($CheckDateB)>>
<<print $args[2]>>

Now you can use this at the top of any passage in Twine to increase the time by the minutes of your choice. You can put it at the top of the page

<<minutes_incr 90>>

or in a link

<<link "link text">><<minutes_incr 90>><<goto "Start">><</link>>

And in any passage, you can use this to do something if the date and time are within a certain time frame.

This checks whether something is between two times and makes something appear. Note the 24 hour clock (don’t use am and pm). Also note that the first date is inclusive (greater than or equal to) and the second is non-inclusive (less than and not equal to).

<<check_date 'August 20, 1575 00:00:00' 'August 20, 1575 23:00:00' 'Say something like this [[link]]'>>
1 Like

I tried initally adding your code to a new passage, called timeWidget, but it didn’t work there. But when
I added it to my existing StoryInit it works just fine. Thnak you veruy much for that.

Did you tag (not name) that passage with the word widget? That is the proper way to make sure it loads first, but in my experience StoryInit will work as well.

No, I didn’t tag it. Although re-reading your initial response, I see that it was clear that I should have. As it is currently working now, I will leave it alone. Thank you again for your prompt response.

1 Like

There’s more info on why to use the widget tag here, by the way. My luck so far with the StoryInit passage seems to lead to trouble, according to the documentation:

Warning: Widgets should always be defined within a widget -tagged passage—any widgets that are not may be lost on page reload—and you may use as few or as many such passages as you desire. Do not add a widget tag to any of the specially named passages and attempt to define your widgets there.

I’m sorry to be thick, but where do I add the above information about the widget, and is the widget called “minutes_incr”?

I tried just pasting it as it is into the CSS, but it made a mess.

I’m afraid I’m one of those guys who needs to be told the name of where stuff goes, and what bits require my input, otherwise I’m guaranteed to misunderstand the directions given until I’ve done it a few times.

1 Like

The widget passage should look like this. It’s just a matter of adding the word “widget” below the title. It has to be a new passage, not the CSS or any other passage.

I tried adding “widget” to some of my passages, but all that happened was my links no longer worked.

It is currently working with

<<button "Home">><<minutes_incr 30>><<goto "Room 23">><</button>> (30 min)

as each button, and I will just have to add the time manually to each button. The (30 min) at the end is the only way I can think of to tell the player how long each option will take them. It’s not elegant, but I’ll live with it.

Your widget code is in my StoryInit, and that works. The more coding of various types I add, the more code there is for me to review whenever I need to add or change something, and I just don’t feel confident in tinkering with it. I’ll do it the manual slog way, and hopeully in a future game, deal with it again when I know more bout what I am doing.

Thank you for your time.

No problem.

I tried adding “widget” to some of my passages, but all that happened was my links no longer worked.

If I’m understanding right, you tagged existing passages. You need to make entirely separate passages for the widgets…they can’t be story passages.

In other words, a passage tagged widget can only contain widgets.

Oh, so it is a single passage to contain all widgets I might require?

Does the code for the widgets also reside here?

Yes, you can have one passage for the all the widgets. Only the code for the widgets goes inside them…no passage text, etc.

I have finally done it, and it seems to be working. Thank you once again.

1 Like