Revision of Twine: escape line breaks with backslashes from Sat, 05/04/2013 - 21:14

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>>
<<if $hp gt 2>>
<<set $hp -= 1>>
<<else>>
<<set $dead = true>>
<<endif>>
<<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:

<<if $hp gt 2>>/%
%/<<set $hp -= 1>>/%
%/<<else>>/%
%/<<set $dead = true>>/%
%/<<endif>>
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:

<<if $hp gt 2>>\
<<set $hp -= 1>>\
<<else>>\
<<set $dead = true>>\
<<endif>>
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: "escapedLine",
    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