Possible Limitations with (Replace:)?

Twine Version 2.3.7
Story Format: Harlowe 3.1

Newbie here! I’m doing this action where if a reader has already visited a passage, new text would spawn instead of the original. If they keep revisiting, they would get different text, up until 3 visits.

(if: (count: (history:), "Meet the Kitchen") is 1)[(replace: "Original Text.")[New Text]]
(else-if: (count: (history:), "Meet the Kitchen") is 2)[(replace: "Original Text.")[New Text 2.0]]
(else-if: (count: (history:), "Meet the Kitchen") is > 2)[(replace: "Original Text.")[New Text 3.0]]

This works perfect…until the original text has a line break or dialogue in it? If I write:

(if: (count: (history:), "Meet the Kitchen") is 1)[(replace: "Original "Text".")[New Text]]
(if: (count: (history:), "Meet the Kitchen") is 1)[(replace: "Original Text.
Here's a New Line.")[New Text]]

the (replace:) is either seen as an “Unexpected identifier” or it doesn’t take at all, just leaving the Original Text up instead of the New Text. I’ve messed around with it enough to find out that everything goes haywire until I make the original text as one block with no quotations. Is this the limitations of replace or am I possibly writing this wrong? Thanks!

I would advise against using the (replace:) macro to achieve the result you want, when you can do the same with the (if:) macro structure itself. I would also advise using a temporary variable to store the result from the call of the (count:) macro because the (history:) macro needs to generated the array of Passage Names it returns.

The following achieves the result you asked for, as in it shows a different block of text depending on how many times the reader has visited the Library Passage. Just replace the short text in each of the associated hooks with the long text you want shown.

(set: _count to (count: (history:), "Library"))\
(if: _count is 1)[This is your first visit to the library.....]
(else-if: _count is 2)[Back again already???]
(else-if: _count > 2)[You must really like the library....]

Just in case you still wish to use the replace: macro…

The first issue I see is that you have to escape the inner quotation marks (") with a backslash markup.


<!-- this won't work and give you that "Unexpected identifier" error message -->
Original "Text".
(replace: "Original "Text".")[New Text]
<!-- but this will...-->
Original "Text".
(replace: "Original \"Text\".")[New Text]

It is basically the same issue if you have to replace text on different lines. You have to use the backslash markup again but this time to delimitate the different lines.


My first line.
My Second line.

(replace: "My first line.\My Second line.")[Just one blob of text...]

Thank you for the additional tip, my friend! Both solutions that will definitely come in handy for me later!

1 Like

Ah, I see how simple that works, thanks! Would there be a way that the original text disappears with each visit to the library with that if macro structure, as well?

Keeping in the spirit of greyelf’s answer, what you could do is to code a generic Library passage that fetches its text content from other passages depending on the circumstances…


(set: _count to (count: (history:), "Library"))
(if: _count is 0)[(display: "1st visit in library passage")]
(else-if: _count is 1)[(display: "2nd visit in library passage")]
(else-if: _count > 1)[(display: "nth visit in library passage")]

The visible relationship between the host Library passage and the other potentially included passages won’t be shown in the Twine editor though… :frowning:

But the (display:) macro is too cool/flexible to be ignored! :slight_smile: