Forcing the player into a passage after a certain time.

0 votes
asked Dec 2 by martinstoichev99 (120 points)
retagged Dec 2 by martinstoichev99
So, iam using this code to make the ingame time
if (! setup.changeDate) {
  setup.changeDate = function(date, interval, units) {
​
    var d = new Date(date); // Don't change original date.
​
    switch(interval.toLowerCase()) {
      case 'years':
        d.setFullYear(d.getFullYear() + units);
        break;
      case 'quarters':
        d.setMonth(d.getMonth() + 3 * units);
        break;
      case 'months':
        d.setMonth(d.getMonth() + units);
        break;
      case 'weeks':
        d.setDate(d.getDate() + 7 * units);
        break;
      case 'days':
        d.setDate(d.getDate() + units);
        break;
      case 'hours':
        d.setTime(d.getTime() + units * 3600000);
        break;
      case 'minutes':
        d.setTime(d.getTime() + units * 60000);
        break;
      case 'seconds':
        d.setTime(d.getTime() + units * 1000);
        break;
      default:
        break;
    }
​
    return d;
  };
}
​

 


  • ​And my question is how to make it so, for example when the time of each day hit 22:00, to force player to a passage where he can sleep. As i dont quite understand the code, since i copied it ( but it works) iam not sure which is the varriable for case 'hours'
    

 

3 Answers

+1 vote
answered Dec 3 by Chapel (33,870 points)
How are you using this code?  Can we see an example from within you passages?
commented Dec 3 by martinstoichev99 (120 points)
I have typed it in the Edit Story JavaScript tab. The code is creating an ingame time which i can control with <<set $now to setup.changeDate($now, 'minutes', 15)>>   for example, but i want to make it, so when it hits certain time, to force the player into a certain passage.
commented Dec 3 by martinstoichev99 (120 points)
And i have <<set $now to new Date(2017, 11, 1, 9, 0, 0)>>  in the StoryInit passage
0 votes
answered Dec 3 by Deadshot (2,370 points)
I don't know how to Sugarcube very well, but the Harlowe version of what you're looking for is the (goto:) macro.
0 votes
answered Dec 3 by greyelf (43,790 points)

note: The following solution is based on the information you supplied to @Chapel.

You can use the Javascript <Date>.getHours() function to obtain the number of hours that have past since midnight for a particular Date object. You can then use an <<if>> macro to compare that number against 23.

The following example creates a Date object, displays the current time of that object, then uses a <<link>> macro to allow the updating of the objects hours property. The link also use a <<replace>> macro the update the current time being shown, and uses the above mentioned check to cause the forwarding to the other passage via a <<goto>> macro.

<<set $now to new Date(2017, 11, 1, 9, 0, 0)>>

time: @@#time;<<print $now.toTimeString().split(' ')[0]>>@@

<<link "add hour to current time">>
	<<set $now to setup.changeDate($now, 'hours', 2)>>

	/% Update the displayed time. %/
	<<replace "#time">><<print $now.toTimeString().split(' ')[0]>><</replace>>

	/% Check if 23:00, which is the 23rd hour. %/
	<<if $now.getHours() is 23>>
		<<goto "Other Passage">>
	<</if>>
<</link>>

... it should be noted that there are other places that the same check could be used to achieve the same result.

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