Help with different types of random events

Here is how I would approach the problem:

  1. Click the magnifying glass icon in the upper right to get a search bar drop-down.

  2. Enter gregorian date into the search bar. It’s often best to start with a specific term in order not to get irrelevant results. (If there are no results, you can always broaden the search.)

  3. In the search results, the second topic looks very promising, titled [Sugarcube 2.31.1] Events triggered based on calendar date.

  4. That thread seems to provide very useful hints for the usage of the widget. (Although, probably not the exact solution to the days problem.)

  5. So then, I’d look closely at the code of the widget itself, to see whether it contains built-in definitions that would solve the task directly. That doesn’t seem so.

  6. However, from the widget’s setup code (and from a Twinery forum post where TheMadExile provides the widget), we see that the JavaScript Date object is used.

  7. So, I would read up on the JS Date object. TME provided a link in that post, otherwise it’s the first Google hit for JavaScript Date object:
    Date - JavaScript | MDN

  8. That can look fairly intimidating if you aren’t used to it, but we can at first just skim-read it, since we are only looking for a method to calculate days between dates.

  9. We don’t immediately find a promising method name, but at the bottom of the page, there’s a section called “Calculating elapsed time”.

  10. There we see that we can subtract one date from another (or equivalently subtract the values returned by their getTime() methods), and we will get back the time difference in milliseconds. So, to get what that means in days, we will need to divide it by the number of milliseconds in a day (1 day = 24 hours = 1440 minutes = 86400 seconds = 86400000 milliseconds).

  11. As a quicker alternative to steps 7-10, we could have googled javascript calculate days between dates, the first hit is:
    How to calculate the number of days between two dates in javascript? - GeeksforGeeks
    Where we find:

// To calculate the time difference of two dates
var Difference_In_Time = date2.getTime() - date1.getTime();
  
// To calculate the no. of days between two dates
var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);
  1. I’m not well-versed in Twine, but I think for Sugarcube it should be something like:
<<set $gameDate to new Date(2015, 2, 17, 3, 24);>>
<<set $futureDate to new Date(2015, 2, 24, 3, 24);>>

<<set $diffdays to ($futureDate.getTime() - $gameDate.getTime()) / 86400000>>

I hope this helps or provides some necessary building blocks.

1 Like