Twine: escape line breaks with backslashes

Update: The Javascript on this page is now built into Twine 1.4! It is no longer necessary to install it.

As you know, one of the most prominent difficulties with mixing code and prose in Twine games is the handling of line breaks. If you have a bunch of macros interspersed in your prose, especially <<if>> macros, then the temptation is to leave them on separate lines from the text, for readability. However, the line breaks adjacent to these macros end up appearing in the rendered passage.

The <<silently>> macro is the 'official' solution...

<<set $hp -= 1>>
<<set $dead = true>>
Wrap up all of the macros in it and the line breaks will be suppressed. But, it seems to me to be a half-effective solution, requiring two large macro tags enclosing each block, and disallowing <<print>> macros within it.

I've previously advocated a different solution, involving the TiddlyWiki comment syntax:

%/<<set $hp -= 1>>/%
%/<<set $dead = true>>/%
This has the advantage of allowing the use of <<print>> macros. But it, too, is authorially and visually unwieldy, requiring both the end and beginning of lines to be tagged with one of two similar-looking tags.

However, I believe it's on the right track. Right now, my proposal is to directly borrow the syntax of the C language's preprocessor - a single backslash at the end of a line suppresses the line break:

<<set $hp -= 1>>\
<<set $dead = true>>\
You are \
<<if $hp gt 1>>
almost dead
In my opinion, this has much more convenience compared to the other two.
Here is the script code to enable this in your stories:

Obsolete script removed: use Twine 1.4

To recap, when you install this script:

  1. A line that reads:
    You have \
    twenty dollars.
    ...will be rendered as "You have twenty dollars."
  2. A line that reads:
    You have \
    <<set $cash = "twenty">>\
    <<print $cash>>\
    ...will be rendered as "You have twenty dollars."

Feel free to report any bugs to @webbedspace.


spiral's picture

GREAT script! Definitely

GREAT script! Definitely going to be getting a loot of use out of this :) Keep up the awesome work!


Does this require a particular template or version of Twee? I tried using this with Jonah and Twee 1.7.1, and when I try to run the result, it says there's a syntax error in the script.

My test code is here:

I had a look at that test

I had a look at that test code, and... you added a comment and ended it with a period??

Ack, you're right! I don't

Ack, you're right! I don't know *where* that period came from...