Is it possible to measure the amount of time spent in the game (Twine, Harlowe 3.2.3)?

Twine Version: Twine 2
Story Format: Harlowe3.2.3

Hello!

I was wondering if it might be possible to write some code that will track how long a user spends in the game, and report that amount of time at the end (as in, a final congratulations screen telling the user various stats including time taken.)

I tried adding:

(set: $gamestart to (current-time: ))

to my startup passage, and

(set: $gameend to (current-time: ))

to the final passage, thinking that I could just subtract to get the amount of elapsed time.

However, it seems that these variables are being saved as strings rather than numerical values, and I am not clever enough with Harlowe coding to know how to convert datatypes.

Is there any way to do this, or is this just an idea I should abandon?

There’s the (number:) macro https://twine2.neocities.org/#macro_num that allows to change a string to a number. However the format of the string will comprise letters or other signs, like ‘:’ or ‘am’/‘pm’, so it will need some coding to achieve any result. And if the user needs more than one session, you won’t be able to track time this way.

There might be another way, with the time keyword https://twine2.neocities.org/#keyword_time. Here’s a tentative code.
First passage:

(set: $t to time)

Any link to another passage, add time to $t

(link: "Forward")[(set:$t to it + time) (goto: "Next passage")]
1 Like

The (link-reveal-goto:) macro is generally a better choice when you need a what is commonly known as a “Setter” link. as it requires one less macro call to achieve the same outcome.

(link-reveal-goto: "Forward", "Next passage")[(set:$t to it + time)]
2 Likes

Thank you both so much! I’ve gone with the second option as it seemed much simpler - I can see how complicated my original idea was, lol. Thanks a million for pointing me to the time keyword and also to the simpler link-reveal-goto macro (I can tell already that they’ll both be very useful!)