Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

llDialog and \n in a string-variable...

Kenn Nilsson
AeonVox
Join date: 24 May 2005
Posts: 897
01-04-2009 21:02
So I'm runnin' through testing on a script and all is working great ... until I bring up a dialog menu that has \n line-breaks in it:

From: string
Do you wish to clock in?\n\nHourly Rate: L$100\nOvertime Rate: L$100\nMinimum Worktime: 0 h 0 m 0 s\nMaximum Daily: 24 h 0 m 0 s


In the dialog menu, the text above appears --exactly-- that way ... treating the \n as part of the 'sentence' so to speak.

The llDialog command is as follows: llDialog(id,str,lmenu,CH_MENU);

Now ... if I take that --exact same-- string and place it as follows:

llDialog(id,"Do you wish to clock in?\n\nHourly Rate: L$100\nOvertime Rate: L$100\nMinimum Worktime: 0 h 0 m 0 s\nMaximum Daily: 24 h 0 m 0 s",lmenu,CH_MENU);

It works and treats \n as a linebreak.

Thing is ... this message is coming from the web and can be variable. Therefore, I need to keep it in ... well ... a variable. Any ideas from anyone on how to get line-breaks to work in an llDialog command when not inputting the string directly into the function?

BTW ... (edit)

1 - llUnescapeURL doesn't work.
2 - Dumping to a list based on \n and then back to a string with \n separator doesn't work
3 - Typing out a string variable like str="line1\nline2\nline3" DOES work ... but again, this info is coming from the web ... need a way to manipulate it to work from there.
_____________________
--AeonVox--

Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms chasing ghosts, eating magic pills, and listening to repetitive, addictive, electronic music.
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
01-04-2009 21:09
Whatever is sending you that line is sending you a \ character and an n character, not a \n character. Subtle yet important difference..

Something similar came up recently with llSetText. See /54/ca/300035/1.html on some options to replace the \ and n with a single \n.

edit: the short answer is something like:

str = llDumpList2String(llParseString2List(str, ["\\n"], []), "\n";);

From: someone
2 - Dumping to a list based on \n and then back to a string with \n separator doesn't work

Try \\n..

See also: http://wiki.secondlife.com/wiki/String#Escape_Codes
Kenn Nilsson
AeonVox
Join date: 24 May 2005
Posts: 897
01-04-2009 21:15
Thank you much Sindy!

Seems like such a simple solution after you mentioned that :-) I was trying to parse string-to-list with \n ... but using \\n instead makes the whole thing function :-)
_____________________
--AeonVox--

Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms chasing ghosts, eating magic pills, and listening to repetitive, addictive, electronic music.
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
01-04-2009 21:20
:)
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
01-04-2009 21:30
The reason the ecaping works when you put the string directly in your script is that the compiler replaces '\n' character sequences with newline characters in string literals when your code is actually compiled. When you get a string from another script, or from chat, or HTTP reply, or email, or wherever else, it is expected that newline characters are already in the string where needed.

You should see newlines in the dialog as long as the strings coming from your web server actually has NEWLINE characters/sequences in them. If they have literal backslash ('\') characters followed by literal lower-case N characters ('n'), you're right that it will print that character sequence in the dialog. Your choices are to fix the values coming back from the webserver so that they actually contain newlines (and it is okay if your server returns the standard network CRLF--carriage return, linefeed--characters because SL will translate them to newline characters for you before your code sees them), or to do some kind of parsing to allow escaped characters. This could be done with something like:

CODE

// Replaces '\n' sequences with newlines, '\t' sequences with tabs, and all
// other characters following '\' with themselves (so '\\' gets replaced with
// a single backslash character).
string unescapeCharacters(string escaped)
{
string result = "";
integer nextBs = llSubStringIndex(escaped, "\\");
integer charsLeft = llStringLength(escaped);

while (nextBs >= 0 && nextBs < charsLeft-1)
{
if (nextBs > 0)
{
result += llGetSubString(escaped, 0, nextBs-1);
}

string eChar = llGetSubString(escaped, nextBs+1, nextBs+1);
if (eChar == "n")
{
result += "\n";
} else if (eChar == "t")
{
result += "\t";
} else
{
result += eChar;
}

if (nextBs < charsLeft-2)
{
escaped = llGetSubString(escaped, nextBs+2, -1);
charsLeft -= nextBs+2;
nextBs = llSubStringIndex(escaped, "\\");
} else
{
escaped = "";
charsLeft = 0;
}
}

return result+escaped;
}


EDIT: Oops. Sat on this thread long enough without refreshing that I didn't see that someone had answered by the time I finally posted. :o
Yingzi Xue
Registered User
Join date: 11 Jun 2008
Posts: 144
01-05-2009 03:10
Woops... a minor error Hewee. I think this line...

escaped = llGetSubString(nextBs+2, -1);

...is supposed to have this?...

escaped = llGetSubString(escaped, nextBs+2, -1);
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
01-05-2009 08:26
From: Yingzi Xue
Woops... a minor error Hewee. I think this line...

escaped = llGetSubString(nextBs+2, -1);

...is supposed to have this?...

escaped = llGetSubString(escaped, nextBs+2, -1);

True. Fixed above. Thanks.