Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

llSetText from chat; using \n and \"

Yingzi Xue
Registered User
Join date: 11 Jun 2008
Posts: 144
12-29-2008 19:25
I'm writing a small script to allow me to set hover text on an object, then die the script. It works great, but I've found that if you pass \n or \" from chat text it doesn't work. A different format I'm assuming. I can get around it by using my own identifiers which the script replaces in the string with \n and \". Is there a function to convert chat text to allow these to be passed without the workaround?
Ollj Oh
Registered User
Join date: 28 Aug 2007
Posts: 522
12-29-2008 19:33
chattext is phrased and displayed differnetly by different clients.
" " changes into " " and linebreak changes into something new each year.
try copying a linebreak from chattext and see if it pastes into a script to read and convert it.
if not just change "." to ".\n "

" works nicely within a string, you just need \" to get it into a scripts string without a listen, not to confuse the compiler.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
12-29-2008 21:05
This SHOULD do it, but I haven't compiled or tested it so some fixes might be needed.

CODE

// Replaces literal occurances of "\n" in the parameter with newline, and
// literal occurances of "\" followed by any other character with the second
// character. All other characters and sequences are unchanged.
string replaceEscapes(string str)
{
string result = "";
string rest = str;
integer nCharsLeft = llStringLength(str);

while (nCharsLeft > 1)
{
integer nextBs = llSubStringIndex(rest, "\\");
if (nextBs < 0 || nextBs >= nCharsLeft-1)
{
result += rest;
rest = "";
nCharsLeft = 0;
} else
{
if (nextBs > 0)
{
result += llGetSubString(rest, 0, nextBs-1);
}

string escChar = llGetSubString(rest, nextBs+1, nextBs+1);
if (escChar == "n")
{
result += "\n";
} else
{
result += escChar;
}

if (nextBs < nCharsLeft-2)
{
rest = llGetSubString(rest, nextBs+2, -1);
nCharsLeft -= nextBs+2;
} else
{
rest = "";
nCharsLeft = 0;
}
}
}

return result+rest;
}
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
12-29-2008 23:40
...alternatively....

CODE

listen(integer channel, string name, key id, string message)
{
message = llDumpList2String(llParseString2List(message, ["\\n"], []), "\n");
message = llDumpList2String(llParseString2List(message, ["\\\""], []), "\"");
llSetText(message, <1,1,1>, 1);
}


/esc
_____________________
http://slurl.com/secondlife/Together
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
12-30-2008 02:32
From: Escort DeFarge
...alternatively....

CODE

listen(integer channel, string name, key id, string message)
{
message = llDumpList2String(llParseString2List(message, ["\\n"], []), "\n");
message = llDumpList2String(llParseString2List(message, ["\\\""], []), "\"");
llSetText(message, <1,1,1>, 1);
}


/esc


True. I assumed backslash would be used as a true escape, meaning it could also escape itself for a literal backslash character. Your code wouldn't do that, but it WOULD strictly satisfy the original stated requirements.
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
12-30-2008 02:46
From: Hewee Zetkin
True. I assumed backslash would be used as a true escape, meaning it could also escape itself for a literal backslash character. Your code wouldn't do that...

Really? Try it :)

edit: more accurately, you don't need to escape a backslash as it's already escaped by the time it gets to the listen.
_____________________
http://slurl.com/secondlife/Together
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
12-30-2008 11:10
From: Escort DeFarge
Really? Try it :)

edit: more accurately, you don't need to escape a backslash as it's already escaped by the time it gets to the listen.

Yeah. Really. If backslash is being treated as a true escape character, saying "Hello\\nThere" (from CHAT, not from inside a script) shouldn't have a newline in it. It should have a backslash followed by a lower-case N. If the result of THAT were fed back in it would have a newline and no backslashes. But the OP simply asked to be able to put newlines (and quotes) in, which your code certainly allows.
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
12-30-2008 11:46
Doing this perfectly cleanly involves something like...

message = llDumpList2String(llParseString2List(message, ["%"], []), "%1";);
message = llDumpList2String(llParseString2List(message, ["\\\\"], []), "%2";);
message = llDumpList2String(llParseString2List(message, ["\\n"], []), "\n";);
message = llDumpList2String(llParseString2List(message, ["\\""], []), "\"";);
message = llDumpList2String(llParseString2List(message, ["%2"], []), "\\\\";);
message = llDumpList2String(llParseString2List(message, ["%1"], []), "%";);
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore