Activating text within a single passage

Twine Version: 2.3.16
Story Format: sugarcube 2.36

I am trying I suppose to replicate twine passage functionality within a single passage.

What I want to do is have a block of code appear after you make a single link-replace earlier in the passage. Right now, the link “its time to see what you picked” shows up all the time, when obviously I only want that once you have picked a choice.

I had some code in there which would see <<if _picked is ‘(empty)’>> before displaying the last part, but this would not display anything.

The first part of my code already works and picks an option, and replaces the whole thing.

<<set _picked to ''>>/
It is time to make a choice

<span id='choice1'>
<<link "Pick choice A">>
<<replace '#choice1'>>
And I will display choice A text.
<<set _picked to 'choiceA'>>
<</replace>>
<</link>>

<<link "Pick choice B">>
<<replace '#choice1'>>
And I will display choice B text.
<<set _picked to 'choiceB'>>
<</replace>>
<</link>>
</span>

But I want to replace them both when clicked. This I have achieved.


<<linkreplace "Its time to see what you picked">>
<<if _picked is 'choiceA'>>
You picked choice A!
<</if>>
<<if _picked is 'choiceB'>>
You picked choice B!
<</if>>
<</linkreplace>>

Welcome David,

Your <<linkreplace>> is outside the zone you’re replacing (<span id='#choice1'></span>). So it will always appear.
As this macro is the result of a choice, it should be inside both <<link>><</link>>.

Yes that makes sense. Thank you for your response.

Is there any way to avoid pasting the linkreplace passage within both links? The programmer part of me hates re-using identical code like that.

How I would tackle this normally would be to have a <<display ‘passage’>> within both links that contains the linkreplace code. But I am trying to see if I can do this all within one passage.

What I almost want is to contain the linkreplace within a span id, then do something like (display spanid) if that makes sense. I know this doesn’t work.

You can always use a <<widget>> https://www.motoslave.net/sugarcube/2/docs/#macros-macro-widget to avoid repeating the same chunk of code.

Perfect, that is exactly what I was looking for! It hadn’t occured to me to use widgets outside of defining widgets for use everywhere in the project. For reference, my code now looks like this:

<<set _picked to ''>>\
It is time to make a choice

<span id='choice1'>
<<link "Pick choice A">>
<<replace '#choice1'>>
And I will display choice A text.
<<set _picked to 'choiceA'>>
<<temp1>>
<</replace>>
<</link>>

<<link "Pick choice B">>
<<replace '#choice1'>>
And I will display choice B text.
<<set _picked to 'choiceB'>>
<<temp1>>
<</replace>>
<</link>>
</span>



<<widget 'temp1'>>
<<if _picked is ''>>
<<else>>
<<linkreplace "Its time to see what you picked">>
<<if _picked is 'choiceA'>>
You picked choice A!
<</if>>
<<if _picked is 'choiceB'>>
You picked choice B!
<</if>>
<</linkreplace>>
<</if>>
<</widget>>