0 votes
by (710 points)
Hello,

$gameDate is a Date() object set to the current in-game time, and $firstActingLesson is another Date() object currently equal to $gameDate.

Let's say $gameDate is (2018, 5, 14, 15, 30), or June 14, 2018 15:30 pm, as is $firstActingLesson. What would be the best way to change $firstActingLesson so that it is equal to 18:00pm on the following day? I have widgets to add minutes or add hours to either object, but I can't figure out how to tell Twine when to stop adding.

The purpose of this would be so that I can restrict access to passages to specific times - in this case so that the player would be able to go to the acting lesson only when it is supposed to start.

1 Answer

+1 vote
by (36.1k points)
selected by
 
Best answer

As long as you're using the JavaScript Date object, you might as well take advantage of all of the functions for it like this:

<<nobr>>
	<<set $CurDate = new Date('June 14, 2018 15:30')>>
	Current date: <<print getDayName($CurDate)>>, $CurDate<br>
	<<set $Appointment = clone($CurDate)>>
	<<set $Appointment.setDate($Appointment.getDate() + 1)>>
	<<set $Appointment.setHours(18)>>
	<<set $Appointment.setMinutes(0)>>
	<<set $Appointment.setSeconds(0)>>
	Appointment date: <<print getDayName($Appointment)>>, $Appointment<br>
<</nobr>>

NOTE: It's very important that you use the clone() function to copy the date object, or any objects, otherwise changing one object will also change the other for any code that's used in the same passage.  (After going to a new passage in the story the objects are all basically cloned into separate variables.)  This is because objects are normally copied by reference, instead of by value the way numbers, booleans, or strings are.

You'll need to add this function to your JavaScript section if you want to show the name of the day of the week as I did in the above code:

window.getDayName = function(CurDate) {
	return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][CurDate.getDay()];
};

Hope that helps! smiley

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.
...