Showing text when updating passage using <<replace>> and <<include>>

Hello there,

I am using Twine 2.6.2.0 and Sugarcube 2.36.1.
I am trying to make a simple fridge system, and I used the “Simple Update Without Reload” method from HiEv’s sample code to set it up.

I have this in my fridge:

<span id="fridgeFood"><<include "Fridge Food">></span>

and this in Fridge Food:

<<if $junkFood gt 0>>
<<link "Eat junk food">><br>
    <<set $junkFood-=1>>
    <<eatJunk>>
    	<<replace "#fridgeFood">>
        <<include "Fridge Food">>
        <</replace>>
    <<updateCaption>>
<</link>>
<</if>>

Now, what I’m trying to do is that when I click on the link, a line of text appear, and is replaced by another one when I click again, etc…

I tried with a simple line of text and with append, but it does not appear since the passage is refreshed when I click. Does anyone know a way around this? Ive been scratching my head since yesterday to no avail. :sweat_smile:

(I started using Twine a few days ago and I have no prior programming knowledge)

Thank for any help!

At a glance, something in the widgets you’re calling could be causing this - maybe it’s the <<updateCaption>> that refreshes the passage?

If I delete <<eatJunk>> & <<updateCaption>> from your example, it works and refreshes everything, but there is a problem: the link itself is not replaced, so you can click it as many times as you want, decreasing $junkFood into negative values. You should wrap the link in a <<replace>> macro as well:

<<if $junkFood gt 0>>
<span id="foodLink"><<link "Eat junk food">><br>
    <<set $junkFood-=1>>
    
    	<<replace "#fridgeFood">>
        <<include "Fridge Food">>
		        <</replace>>
    	<<if $junkFood lte 0>>
			<<replace "#foodLink">><</replace>>
		<</if>>	
<</link>></span>
<</if>>

note: When you mention a third-party library or method in your question/comment it helps if you supply a link to it, because not everyone will know which you mean. I will assume you are using this Simple Update Without Reload method. I will also assume that you are setting the $junkFood variable to a number greater than 0 (zero) sometime prior to the Passage that contains the identified <span> element.

There are a couple of issues with your existing Fridge Food Passage example:

  1. If you read the <<link>> macro’s documentation you will learn that the link it creates executes its contents silently, which means any visual output like the <br> element you placed within that macro’s body wont be displayed in (or added to) the web-page.
  2. Besides the Eat junk food link your Passage doesn’t include any visual output to display in the page when the passage is include.
    Unlike the method’s Event Passage passage which displays a = _a<br> and b = _b<br> on the page. You will notice that both of these things are placed outside the <<link>> macro’s content.

So if you want your Fridge Food Passage to display a “line of text” then you need to add that content outside of the <<link>> macro call.
eg, the following displays a series of comments about the amount of food left…

<<if $junkFood gt 0>>
	<<link "Eat junk food">><br>
		<<set $junkFood -= 1>>
		<<eatJunk>>
		<<replace "#fridgeFood">><<include "Fridge Food">><</replace>>
		<<updateCaption>>
	<</link>>
	<<if $junkFood gt 3>>
		You have $junkFood units of Junk Food.
	<<elseif $junkFood is 3>>
		You only have $junkFood units of Junk Food, better buy some more!
	<<elseif $junkFood is 2>>
		Only $junkFood units of Junk Food left!
	<<elseif $junkFood is 1>>
		You're down to your last unit of Junk Food!
	<</if>>
<</if>>

@agat
Thank you for your answer.
The <<eatJunk>> is to update some stats and the <<updateCaption>> is to refresh the caption and see the updated stats, so they shouldn’t have an impact within the passage. And I admit I did not think about clamping $junkFood since the link disappears once the variable reaches 0, but I’ll probably do that to prevent it from happening if I make a mistake somewhere else.
But my problem is still the same, I don’t know if my post was very clear: when I click the link, on top of everything refreshing, which is working as intended as far as I can tell, I would like to have a line of text appearing under the link, something like “Better hit the gym after that…”. Then, when I click again, another line of text replaces the first one - using <<switch random()>> for example - and so on until the link disappears once the $junkFood variable reaches 0. I don’t know if I’m clear :sweat_smile:

@Greyelf
:woman_facepalming: I did not realise I was always putting the text inside the <<link>> macro. Now I’ll try to do it so that the text only appears after I clicked on the link, but I should manage with the input you gave me. Thank you a lot!

I think <<include>> is what you’re looking for, for the text at the bottom.

Here is code for something similar from a game I did. Maybe it will help:

<span id="textBox">
<<print setup.tv.messages.random()>>
</span>
<br>
<<link "change channel">>
 <<replace "#textBox">>
   <<include "watchtv">>
  <</replace>>
<</link>>

and the passage watchtv:

<<print setup.tv.shows.random()>>
<br>
<<link "change channel">>
 <<replace "#textBox">>
   <<include "watchtv">>
  <</replace>>
<</link>>
1 Like