New Line Inside a String Variable

349 views
Skip to first unread message

9of9

unread,
Aug 28, 2013, 4:27:08 PM8/28/13
to twee...@googlegroups.com
Is there any way of having special characters such as newline inside of string variables?

Something like:

<<set $testVar = "Line 1.

Line2."
>>

Results in a whole mess of errors. While writing something like '\n' gets replaced in the output with just 'sn'. Is there any way of specifying special characters inside of string variables? I would quite like to be able to save larger passages of text to variables - e.g. an item description.

Early Day

unread,
Aug 29, 2013, 3:20:40 AM8/29/13
to twee...@googlegroups.com
a quick fix might be something like this
<<set $testVar = 1>>

then create a passage with the extra line you want to display
:: filler passage
"Line 1.

Line2."

then in your actual passage
<<if $testVar = 1>><<display "dummy passage">><<else>>some single line<<endif>>

9of9

unread,
Aug 29, 2013, 7:58:26 AM8/29/13
to twee...@googlegroups.com
Oh yes, that's certainly a possibility. In fact, I'm pretty sure you could even do

<<set $testVar = "dummy passage">>

<
<display $testVar>>

Which would be a spot neater, still. It's not a case of there not being a workaround, I'm just curious if there is a way of having \n and other special characters in strings - and if not, then why not =)

HarmlessTrouble

unread,
Oct 14, 2013, 2:58:53 PM10/14/13
to twee...@googlegroups.com
There's some discrepancy between the way Twine converts newlines for storage and the JS story engine converts it for use.  I've been able to compose twine stories in a prototype browser based editor I've been working on. (simple text, nothing as cool as Twine 2).  And noticed I wasn't having the same problems. 

Checking the API, the function that unescapes slashes and newlines is.
unescapeLineBreaks
Passage.unescapeLineBreaks = function ( text )

checking the code in the engine.  The script appears to do what it needs to. 


Passage.unescapeLineBreaks=function(A){if(A&&A!=""){return A.replace(/\\n/mg,"\n").replace(/\\/mg,"\\").replace(/\r/mg,"")}else{return""}};


But no sign of the extra s.  

- and I think I found it.. In Twine's tiddlywiki.py  the python script that converts the story for storage. 

Line 436 
output = output.replace('\\', '\s')

So the question is what was the motivation behind '\s' instead of  '\'  ? 

And what are the implications of "fixing" this line?

- Henry

HarmlessTrouble

unread,
Oct 14, 2013, 3:01:51 PM10/14/13
to twee...@googlegroups.com
hermn there's also "Decodes a string from HTML"

line 466 
output = output.replace('\s', '\\')

HarmlessTrouble

unread,
Oct 14, 2013, 3:54:27 PM10/14/13
to twee...@googlegroups.com
Heh, So I learned that commenting out those two lines (439 and 466) results in backslashes rendering normally.  however they're converted into newlines if next to a n as in "\n".  This is fine unless of course you want a literal backslash infront of "n".  Which is I guess one reason for replacing \\ with "\s".

So it turns out the unescapelinebreaks function is  to blame for the mystery "s".  problem. It probably should actually read in the scripts for all targets (header.html for all story formats) as follows.

Passage.unescapeLineBreaks=function(A){if(A&&A!=""){return A.replace(/\\n/mg,"\n").replace(/\\s/mg,"\\").replace(/\r/mg,"")}else{return""}};

Unfortunately this cannot be modded in a script passage.  This will have to be fixed in the targets (header.html) files themselves.   

- Henry

Mike Snyder

unread,
Oct 17, 2013, 8:25:15 AM10/17/13
to twee...@googlegroups.com
This is what I've been doing for embedding linebreaks:

This, somewhere globally:

<<set $newLine = String.fromCharCode(13) + String.fromCharCode(10)>>

Then this in the passage:

<<print $newLine>>
or
<<set $whatever = "This is a test." + $newLine>>
<<print $whatever>>

Reply all
Reply to author
Forward
0 new messages