The simpiest example of in-game time mechanic (SugarCube2)

Greetings. I’d like to write an easy macro for in-game time, but I’ve faced my JavaScript-knowledge-troubles.
The thing is that I want macro to contain all conditions and be active in all passages of the game, for time to go. Also I want to write each time: how much time passed after going from one passage to another.
I wrote this:

macros.timeconditions = {
“handler” : function() {
var year = 135, month = 1, day = 15, hour = 12, minute = 5
if (minute == 60) {hour = hour + 1}
if (hour == 24) {day = day + 1}
if (day == 31) {month = month + 1}
if (month == 13) {year = year + 1}



And I was waiting for it to work, when I wrote:

Date: <<print $year>>, <<print $day>> <<print $month>>, <<print $hour>>:<<if $minute < 10>>0<><<print $minute>>

But it didn’t.
About time passing: in my plan, when macro works and gamer go to next passage, first line in which will be:

<<set $minute = $minute + 5>>

Time will be changed from 12:05 to 12:10.
Can anybody explain: what where my mistakes and what did I probably miss?
Twine Version: 2
Story Format: SugarCube 2

That’s not how macros or JavaScript variables work in SugarCube. If you want to create a macro, then you should take a look at the Macro API section of the SugarCube documentation.

As for time mechanics, you might want to check out the “Time” section of my Twine/SugarCube sample code collection for help with that. It shows you a couple of ways you can use the JavaScript Date object to make tracking times and dates a lot simpler.

There are also several examples of macros in that sample code collection. I’d recommend checking out the “Class” Macro section if you want help understanding the basics of creating and using macros. Then you could take a look at some of the other macros there for more advanced examples.

Hope that helps! :grinning:

P.S. If you’re going to post code in this forum you should do it within a “Preformatted Text” block. Just select the code and click the </> button in the editor window before posting it. If you don’t do that then the forum will tend to “eat” the inside of some macros, leaving only <> in your post.

1 Like

Thank you a lot for navigation, HiEv.I will check and learn.