Using <<replace>> outside of a <<link>>

Twine Version: 2.4.1

I am trying to use the replace macro to update text in the same passage but am getting an error. A simple example is:

<span id="test">
<<include "testText">>

<<include "aPassageThatMayChangeTestText">>

<<replace "#test">><<include "testText">><</replace>>

This gives an error of:
Error: no elements matched the selector “#test

If I wrap the same code in a link it works:

<<link "Refresh stuff">><<replace "#test">><<include "testText">><</replace>><</link>>

I don’t understand why this does not work. Is there a better way to do this?

Thanks for any advice.

You could put your <<replace>> code in the PassageDone special passage for this to work, however, this would mean the original (pre-replace) version of the passage never gets seen. Normally, if you want both the old and new version visible at some point, you’ll use some trigger for the replace macro, like a link. So, if you give a specific example of how the passage is supposed to function, someone may be able to offer a better solution.

(edit: re-reading your example more carefully, I see from the passage names you want to keep the replaced text updated according to the other included passage upon each rendering of yet another passage. In which case, using PassageDone should do the job – although if “aPassageThatMayChangeTestText” just sets a variable to be displayed in “testText”, and does not include any text to display on its own, you could just as well include it first, before including “testText”).

(edit: <<timed>> is likely not what you’re looking for, but I’ll leave it here in case anyone finds it useful

If you want the text to change after some time, you could use the <<timed>> macro:

<<timed 3s>><<replace "#test">><<include "testText">><</replace>><</timed>>

But then there’s always the case of reading speed - the text will be either too fast for some people or too slow for others, or both.


Ok, PassageDone works perfectly thanks :grinning:

For anyone else coming here with a similar issue this thread was also very useful.

The <<done>> macro also will work for this, ie:

The contents enclosed within the <<done>> macro will be executed after everything else has loaded similar to the way the PassageDone special passage works except it only gets called specifically where the <<done>> macro is used and not after every passage is rendered.

1 Like