Revision of Twine: escape line breaks with backslashes from Wed, 12/11/2013 - 03:02

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...

<<silently>>
<<set $hp -= 1>>
<<set $dead = true>>
<<endsilently>>
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>>
hale
<<else>>
almost dead
<<endif>>.
In my opinion, this has much more convenience compared to the other two.
Here is the script code to enable this in your stories:
(function(){
var bs = String.fromCharCode(92);
Wikifier.formatters.unshift({
    name: "continuedLine",
    match: bs+bs+"s",
    handler: function(a) {
        a.nextMatch = a.matchStart+3;
    }
});
}());

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>>\
    dollars.
    ...will be rendered as "You have twenty dollars."

Feel free to report any bugs to @webbedspace.

pensive-mosquitoes