0 votes
by (710 points)

I am using the timed replace macro set (https://www.glorioustrainwrecks.com/node/5029) to have new lines of text appear after a timed delay. Is there any way to let the player interrupt this process halfway through by clicking a link?

For example, if the code was:

<<timedreplace 2s >>You see <<gains>>a dog, <<gains>>a walrus, <<gains>>and a civet.<<endtimedreplace>>

Would it be possible to stop at "a walrus" if the player clicks the link before "a civet" is printed. I would also use that link to launch a different series of strings using another timedreplace.

1 Answer

+1 vote
by (154k points)
selected by
 
Best answer

Please use the Insert Code Snippet button in the toolbar to add code examples to your posts, it makes them easier to find and read.

Firstly, the code found on the Glorious Trainwrecks site is designed to be used with default story formats that come with the Twine 1.x application, it should NOT be used with the SugarCube story formats.

You should be using the using the <<replacelink>> macro set that can be downloaded from on the SugarCube instead.

Secondly, your example doesn't contain a link so where exactly would the link appear?

by (710 points)
edited by

This is along the lines of what I am trying to do. Ideally, the click would interrupt the first set of strings (so if the player clicks after orange, pear would no longer print and instead carrot would print).

<<set $fruits to ["apple","orange","pear"]>>
<<set $vegetables to ["carrot","cabbage","potato"]>>

<<for $i = 0; $i < $fruits.length; $i++>>
<<timed 2s>>
<<print either($fruits)>>
<</timed>>
<</for>>

<<click "__Vegetables__">>
<<for $i = 0; $i < $vegetables.length; $i++>>
<<print either($vegetables)>>
<</for>>
<</click>>

At the moment, <<timed>> is not doing what I want it to do (create a 2 second delay between each fruit), and clicking on vegetables isn't doing anything at all.

by (154k points)

Try replacing your TwineScript with the following...

<<set $fruits to ["apple","orange","pear"]>>
<<set $vegetables to ["carrot","cabbage","potato"]>>
<<set _count to 0>>
<<set _showFruit to true>>

@@#list;@@

<<link "__Vegetables__">>
	<<set _count to 0, _showFruit to false>>
<</link>>

<<silently>>
	<<repeat 2s>>
		<<if _showFruit>>
			<<if _count lt $fruits.length>>
				<<append "#list">><<print either($fruits)>><br><</append>>
				<<set _count++>>
			<<else>>
				<<stop>>
			<</if>>
		<<else>>
			<<if _count lt $vegetables.length>>
				<<append "#list">><<print either($vegetables)>><br><</append>>
				<<set _count++>>
			<<else>>
				<<stop>>
			<</if>>
		<</if>>
	<</repeat>>
<</silently>>

.. it uses the <<repeat>> macro to create the delay between each of the items being shown, and uses an ID element combined with the <<append>> macro to control where the items appear within the page. A _count temporary variable is used to track how many of a specific category of item has need shown, and the _showFruit temporary variable indicates if the loop should keep displaying Fruit.

Clicking in the link resets the _count variable so that 3 random vegetables will be shown.

warning: because you are using the either() function to randomly obtain an item from the relevant array, it is possible that the same items will be show more than once. If you don't want that to happen then you might want to switch to using the <Array>.pluck() function instead.

This can be done by simply replacing the two <<append>> macro calls in my example with the following two calls.

<<append "#list">><<print $fruits.pluck()>><br><</append>>

<<append "#list">><<print $vegetables.pluck()>><br><</append>>

 

by (710 points)
Thank you. I have one question about the vegetables link. At the moment it only works if the player clicks while the fruit loop is still printing. Is there any way to make the vegetables link print vegetables even if the player clicks after all of the fruits have finished printing?
by (154k points)

warning: Unlike my previous example the following code hasn't been tested but it should work.

1. Add a new variable initialisation to the top of the passage, it will be used to track if the main <<repeat>> macro has stopped.

<<set _repeatStopped to false>>

2. Update this new variable just after the <<stop>> macro is called within Fruit related code.

			<<if _count lt $fruits.length>>
				<<append "#list">><<print either($fruits)>><br><</append>>
				<<set _count++>>
			<<else>>
				<<stop>>
				<<set _repeatStopped to true>>
			<</if>>

3. Change the link related code to start a vegetable related <<repeat>> if the Fruit related loop stopped before the link was selected.

<<link "__Vegetables__">>
	<<if _showFruit>>
		<<set _count to 0>>
		<<if _repeatStopped>>
			<<repeat 2s>>
				<<if _count lt $vegetables.length>>
					<<append "#list">><<print either($vegetables)>><br><</append>>
					<<set _count++>>
				<<else>>
					<<stop>>
				<</if>>
			<</repeat>>
		<<else>>
			<<set _showFruit to false>>
		<</if>>
	<</if>>
<</link>>

 

by (710 points)
You have arrived at the store. You start off by buying some fruit.
<<silently>>
<<set $fruits to ["apple","orange","pear"]>>
<<set $vegetables to ["carrot","cabbage","potato"]>>
<<set $nuts to ["walnut","hazlenut"]>>
<<set _count to 0>>
<<set _showFruit to true>>
<<set _showVegetables to true>>
<<set _showNuts to true>>
<</silently>>
@@#list;@@

<<fadein 2s 4s>>
<<link "__Fruits__">>
	<<set _showFruit to true, _showNuts to false, _showVegetables to false>>
<</link>>
<<link "__Vegetables__">>
	<<set _showFruit to false, _showNuts to false, _showVegetables to true>>
<</link>>
<<link "__Nuts__">>
	<<set _showFruit to false, _showVegetables to false, _showNuts to true>>
<</link>>
<<link "__Check Out__">>
	<<set _showFruit to false, _showVegetables to false, _showNuts to false>>
<</link>>
<</fadein>>

<<silently>>
	<<repeat 2s>>
		<<if _showFruit>>
			<<append "#list">><<print either($fruits)>><br><</append>>
		<<elseif _showVegetables>>
			<<append "#list">><<print either($vegetables)>><br><</append>>
		<<elseif _showNuts>>
			<<append "#list">><<print either($nuts)>><br><</append>>
		<<else>>
			<<append "#list">><<print "You are done shopping">><br><</append>>
			<<append "#list">>[[another passage|another passage]]<br><</append>>
			<<stop>>
		<</if>>
	<</repeat>>
<</silently>>

Thank you. With your help, I was finally able to get the passage the way I want it!

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.
...