How to make a clickable, repeatable countdown timer in Twine Harlowe 2.0?

0 votes
asked Jan 1 by bennconn (390 points)

I've gotten stuck at wanting to create a clickable, repeatable countdown timer in my Twine Harlowe 2.0 project.

To clarify, I'd like to have a link in a passage that would start a countdown timer in that passage, that, when reaching 0, would allow me to click that same link again to start the same timer again without having to leave the passage. 

I've started by using the following:

(set: $counter to 10)
You have |amount>[$counter] seconds left!

(live: 1s)[
	(set: $counter to it - 1)
	(if: $counter is 0)[(stop:)]
	(replace: ?amount)[$counter]
]

to create a perfect, working countdown timer; but I don't understand how to make it start again with the predetermined value of 10 seconds without having to leave the passage.

I've experimented with the (link-repeat:) macro (alone and combined with a (display:) macro to pull the timer from a different passage after I click the link), I've tried to use a (replace:) macro to no avail, and I've tried anything else I can think of with my limited coding knowledge, but I am just stuck on making it clickable and repeatable. Hopefully I'm making sense; and any help in the matter would be deeply appreciated because I have no idea where to go from here. 

Thank you!

1 Answer

+1 vote
answered Jan 1 by greyelf (58,090 points)
selected Jan 1 by bennconn
 
Best answer

You use a named hook to indicate where to show the link and the countdown message, the (display:) macro to execute the Logic of the countdown. and the (replace:) macro to show the link / message.

1. Place the following in the Passage you want the countdown link (and messages) to appear in.

|countdown>[(display: "Countdown Logic")]

2. Place the following within a new Countdown Logic Passage.

(replace: ?countdown)[\
	(link: "Start the countdown.")[
		(set: $counter to 10)
		(replace: ?countdown)[You have $counter seconds left!]
		(live: 1s)[
			(set: $counter to it - 1)
			(if: $counter is 0)[
				(stop:)
				(replace: ?Countdown)[(display: "Countdown Logic")]
			]
			(else:)[
				(replace: ?countdown)[You have $counter seconds left!]
			]
		]
	]\
]

note: you can rename the Countdown Logic Passage to whatever makes sense to you, as long as you update the two (display:) macros to used the new Passage name.

commented Jan 1 by bennconn (390 points)
That's exactly what I was missing and needed! Thank you so much greyelf!
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.
...