Using time and date to set events

Just so I understand, adding:

<<check_date 'August 20, 1575 00:00:00' 'August 20, 1575 23:00:00' 'The shop is open, [[click here to enter]]' 'The shop is closed'>>

means that between 00:00:00 and 23:00:00 the shop is open?

Does the code work if I remove the date and just leave the time? Because I’m not sure why the date is needed, especially if I can’t track weekends.

Also, I notice that the code above pushes all other text and buttons down the screen by several lines. How does that work? How do I amend that?

Hmm, that is an oversight on my part, we have to create a new widget

<<widget "check_hour">>
<<set $CheckHourA to $args[0]>>
<<set $CheckHourB to $args[1]>>
<<if $CurDate.getHours() gte Math.trunc($CheckHourA) and $CurDate.getHours() lt Math.trunc($CheckHourB)>>
<<print $args[2]>>
<<else>>
<<if $args[3] neq undefined>><<print $args[3]>><<endif>>
<<endif>>
<</widget>>

Then use it like

<<check_date '0' '23' 'The shop is open, [[click here to enter]]' 'The shop is closed'>>

Re: pushing text down the screen, you will have to use nobr (short for no line break) to prevent this from happening and to collapse the relevant lines. There are three options

  • Tag the relevant passage with nobr
  • Wrap the relevant text in <<nobr>>...</nobr>>
  • Add Config.passages.nobr to a passage tagged script

I prefer the third option, as it removes line breaks from the entire story. Then you can add line breaks into your story manually just by typing <br> at the end of a line

There are three options

  • Tag the relevant passage with nobr
  • Wrap the relevant text in <<nobr>>...</nobr>>
  • Add Config.passages.nobr to a passage tagged script

I prefer the third option, as it removes line breaks from the entire story. Then you can add line breaks into your story manually just by typing <br> at the end of a line

Adding a tag script to a passage. Does that require me to add the Config.passages.nobr to a widget or some other passage, like making a passage named script and add it there first?

Sorry for being thick, but that is my default setting.

Create a new passage…you can name it anything. On the line below the title, click the tag plus button and type “script”.

The end result should look like this if you are using the web version of Twine.

I get an error with the time/date code.

I have a passage called test with:

<<button "&ensp; Go home &ensp;">><<minutes_incr 0>><<goto "Room 23">><</button>>
<<check_date '0' '23' 'The shop is open, [[click here to enter]]' 'The shop is closed'>>

and in widgets, I have this:

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

/*<<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]>>
<<endif>>
<</widget>>*/

<<widget "check_hour">>
<<set $CheckHourA to $args[0]>>
<<set $CheckHourB to $args[1]>>
<<if $CurDate.getHours() gte Math.trunc($CheckHourA) and $CurDate.getHours() lt Math.trunc($CheckHourB)>>
<<print $args[2]>>
<<else>>
<<if $args[3] neq undefined>><<print $args[3]>><<endif>>
<<endif>>
<</widget>>

But all I see is: Error: macro << check_date>> does not exist.

On the Room 23 passage, which is the home passage, I had your track birthday code:

<<check_date 'August 21, 1575 00:00:00' 'August 21, 1575 23:00:00' 'Its your Birthday!'>>

Which also displays the: Error: macro << check_date>> does not exist.

If I remove the /* */ from your previous code widget “check_date” there is no error message, but the button to Go Home no longer functions. It throws up an error message: Error: Invalid time value with a long stack trace.

If I move the /* */ from your previous code widget “check_date” to the current “check_hour” it throws up the error message: Error: Invalid time value with a long stack trace.

I’m guessing that the check_date and check_hour codes that I have in widgets, clash with each other, but again I don’t know enough about coding to clean it up myself.

I take it you are trying to comment out each widget to see which one is causing the error in isolation? That would explain the I am not sure why you commented out the check date widget with /* */ but that is probably why you are getting the macro does not exist error.

It does not look like you have updated or editing the first widget so it should still work.

However, I think that you can’t use the widget twice in the same passage due to the way I used $CheckHourA and $CheckHourB. Are you checking twice in the same passage?

Yes, sorry if I wasn’t clear before. I am trying to be scientific in figuring out my problem. When using the:

<<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]>>
<<endif>>
<</widget>>

I don’t see an immediate error, but nothing else relating to time or date appears on the page, and when I click on the button to return, I get Error: Invalid time value with a long stack trace. Plus the button doesn’t take me anywhere, I am stuck on that page.

When I use the:

<<widget "check_hour">>
<<set $CheckHourA to $args[0]>>
<<set $CheckHourB to $args[1]>>
<<if $CurDate.getHours() gte Math.trunc($CheckHourA) and $CurDate.getHours() lt Math.trunc($CheckHourB)>>
<<print $args[2]>>
<<else>>
<<if $args[3] neq undefined>><<print $args[3]>><<endif>>
<<endif>>
<</widget>>

I get the Error: macro << check_date>> does not exist message…

I am using this:

<<check_date '0' '23' 'The shop is open, [[click here to enter]]' 'The shop is closed'>>

and I have this in the StoryInit:

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

The widgets passage has a widget tag.

I also have an extra code passage with

Config.passages.nobr = true

StoryCaption has:

Date: <<print $CurDate>>

I don’t know if any of that is relevant, but that’s all I can find that might be in conflict.

Oh! It’s a simple oversight, I think. Just change check_date to check_hour

<<check_date '0' '23' 'The shop is open, [[click here to enter]]' 'The shop is closed'>>

should be

<<check_hour '0' '23' 'The shop is open, [[click here to enter]]' 'The shop is closed'>>
1 Like

That worked for getting rid of the errors, but the birthday didn’t show up:

<<check_hour 'August 21, 1575 00:00:00' 'August 21, 1575 23:00:00' 'Its your Birthday!'>>

And just to be clear, this one:

<<check_hour '0' '23' 'The shop is open, [[click here to enter]]' 'The shop is closed'>>

means that between 0:00 hours and 23:00 hours is the first option, and otherwise it is the second option. Do I have that correct?

I have included

Config.passages.nobr = true

into my story as shown in your image, but I’m not seeing any removal of blank/empty lines.

Am I missing a step? Do I need to include the name of this passage (I’ve called it extra code) as a tag?

My mistake, looks like the web version/Twine 2 is a bit different. Delete that from the script passage.

In the bottom left corner there is a small arrow, from there click “Edit Story javascript”
Then enter the code there:

Config.passages.nobr = true;

1 Like

That works now. I will have to go through each passage and add the < br > as you mentioned, but that is perfectly fine.

1 Like

@pbparjeter ith the changes we made below, I tried changing:

<<check_date 'August 21, 1575 00:00:00' 'August 21, 1575 23:00:00' 'Its your Birthday!'>>

to

<<check_hour 'August 21, 1575 00:00:00' 'August 21, 1575 23:00:00' 'Its your Birthday!'>>

but nothing shows up. The time/date works in all other regards, but not this one yet. Is it the difference in using the ‘0’ ‘23’ as opposed to the full ‘August 21, 1575 00:00:00’ ‘August 21, 1575 23:00:00’?

Not trying to second guess you, but hoping to get a better understanding of this so I don’t have to bombard you with questions.

Yes, check date needs the full date

<<check_hour 'August 21, 1575 00:00:00' 'August 21, 1575 23:00:00' ...

check hour needs just the hour numbers

<<check_hour '00 '23' ...

It’s possible I posted the widget code incorrectly when I copied it from own game, I will try again later if this doesn’t work for you

So because of the previous issue, I can’t have both set up? Or can we rewrite the check_date so it doesn’t conflict with check_hour?

(I say ‘we’, but we both know I mean ‘you’) :grin:

Basically you include both widgets in your widget passage. When you have widget in front you are defining the widget, once and only once.

<<widget "check_hour"...
<<widget "check_date"...

Then you trigger it without the word widget in normal story passages. You can use these more than once, but only once per passage

<<check_date ...
<<check_hour...
1 Like

Again, it now works perfectly. Now on last question (I hope) related to this.

Is it possible to set a date eg:

<<check_date 'September 1, 1575 00:00:00' 'August 21, 1575 23:00:00' 'Pay Rent'>>

and add some code to automatically deduct the rent, say 100 gold.

<<set $Gold to $Gold - 100>>

I set up this on a passage dedicated to testing:

<div class="row">
 <div class="column2">
  <h3>Lust & Shame</h3>
   <<button "&ensp; Test + 1 Hour &ensp;">><<minutes_incr 60>><<goto "Test Time & Date">><</button>><br>
   <<button "&ensp; Test - 1 Hour &ensp;">><<minutes_incr -60>><<goto "Test Time & Date">><</button>><br>
<br>
   <<button "&ensp; Test + 1 Day &ensp;">><<minutes_incr 1440>><<goto "Test Time & Date">><</button>><br>
   <<button "&ensp; Test - 1 Day &ensp;">><<minutes_incr -1440>><<goto "Test Time & Date">><</button>><br>
</div>

I tried this:

<<check_date 'September 1, 1575 00:00:00' 'August 21, 1575 23:00:00' 'Pay Rent'>><<set $Gold to $Gold - 100>><br>

but it deducted 100 gold with very click to add or subtract time. Way too much rent to pay :slight_smile:

and then I tried this:

<<check_date 'September 1, 1575 00:00:00' 'August 21, 1575 23:00:00' 'Pay Rent'<<set $Gold to $Gold - 100>> >><br>

but it didn’t do anything at all, except to display the second pair of >> from the end of the code, so I knew it was the wrong approach.

First it’s best to format the calculation like this

<<set $Gold -= 100>>

The way you formatted it will decrease the gold every time you visit the passage, as you noted.

The check_date widget will not really be useful because, presumably, you want recurrent payments and do not care what day they on. Here’s an straightforward way to make payments available every thirty days.

In your widgets passage, make new widget called rent_check

<<if Math.trunc($CurDate) gte Math.trunc($NextRentDate) or $NextRentDate eq undefined>>
<<link "Pay rent">>
<<set $Gold -= 100>>
<<set $NextRentDate to Math.trunc($CurDate) + 30*24*60*60*1000>>
<<goto `passage()`>>
<</link>>
<<else>>
Rent up to date, do not pay
<<endif>>

Now you can type <<rent_check>> on any page and it will show the link that deducts the gold and refreshes the passage.

Handling what happens when the player does not pay rent is a different matter…

1 Like

Thank you, once again. I’ll try to work out something in game about rent not being paid.

No doubt that will result in another question or two for you.