+1 vote
by (370 points)
retagged by
I am using Twine 2.1.3 and I want to know if it's possible to implement a timer that decreases over time throughout the whole game. I also want to know how to stop the timer and re-implement it again when the situation asks for it. Is it possible to make one with Harlow 2? Could you also include both countdown and no countdown examples too, if possible?

2 Answers

+1 vote
by (6.2k points)

You can use the (live:) macro:

(set: $time to 10)
(live: 1s)[(set: $time to it - 1)
(if: $time is 0)[(stop:)]]

This will decrease the timer by 1 every 1 second. Once it hits zero, it will stop and the $time variable will remain on zero. Then you can continue the (if:) statement to say 'Game Over' or something.

by (370 points)
Hi Deadshot, I used your code, but it didn't work the way I wanted it to work. It only works on one passage. It doesn't decrease over time throughout the Twine game. Do you know another way to do that? Thank you for attempting to answer this question!
by (6.2k points)
Put the code in all the passages you want to decrease time in. Variables don't reset every time you visit a new passage, so it should work. Also, you could put the code with the (stop:) macro in a footer tagged passage is, instead of having to put that in every time. Do you know how to do footer passages?
by (370 points)
edited by
I don't actually, how does it work? Is it to do with the tag? Like startup?

I tried putting the code in every passage, but nothing happens. Not even within 2 seconds. Am I doing something wrong? Even the first passage that has the code doesn't work anymore.
by (6.2k points)
Footer passages are the same as the startup ones, except you add a tag called 'footer'. Basically, anything you put in it will be at the bottom, so it runs all the code in the passage then does that last. I also like to use it as a place for health bars and stuff. You could also put things like health bars in the 'header' tagged passage. Anything within a passage with the 'header' tag will go at the top.

How isn't the code working? Is the time not decreasing? Is nothing happening once the timer reaches 0 or whatever number you want it to reach. Also, are you sure that you set the $time variable (or whatever you called it) to something at the start?
by (370 points)
Oh! It works now! Thank you so much! I was also wondering if you could include a code where it counts down too?
by (6.2k points)
(live: 1s)[(set: $timer to it - 1)]
(live: 1s)[$timer]

This would show the variable $timer as text, which would be counting down thanks to the first line. However, once a passage is loaded, it isn't constantly reloaded, even if some of the text should change, which is why there's a second (live:) macro - that would reload the text every second.

You could try this one instead, however if you wanted to use the (save-game:) macro, it wouldn't let you, because it's too complicated to save anything or something:

(live: 1s)[(set: $timer to it - 1)(replace: ?timer)[$timer]]
|timer>[$timer]

That would replace the text with the decreased version of itself every second, but this wouldn't let you save, and it's longer anyway, so I recommend the first one.

by (159k points)

WARNING: Timer based handlers like the (live:) macro interrupt and interfere with the Reader's ability to interact with the page whenever the related timer event fires / executes, so it is generally a good idea to only ever use a maximum of one per passage / page to reduces the amount of interference.

+1 vote
by (6.2k points)
Remember to accept an answer so that people can see that this question is answered.
...