Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: playsteven on June 14, 2016, 09:43:14 AM

Title: An issue with a new line "\n" in string[SOLVED]
Post by: playsteven on June 14, 2016, 09:43:14 AM
Hey there,

I've created a simple action to pass a string for new text to TextMesh Pro, and I'm having issues with "\n" in the string:

say the string is "hello\nthere"

Code: [Select]
textmeshPro.text = stringToUse.Value;

displays:

hello\nthere

but if I change the action

Code: [Select]
textmeshPro.text = "hello\nthere";


displays:

hello
there

I've also tried

Code: [Select]
string messageToTry = stringToUse.Value;
textmeshPro.text = messageToTry;

displays:

hello\nthere

Why the difference when the string contents are exactly the same?

Thanks,
Steven
Title: Re: An issue with a new line "\n" in string
Post by: playsteven on June 14, 2016, 09:54:33 AM
I followed the advice at http://hutonggames.com/playmakerforum/index.php?topic=3637.0

Done a string replace "\n" with the new line character and it works a treat.

Still, wouldn't mind knowing why this happens  :)
Title: Re: An issue with a new line "\n" in string
Post by: jeanfabre on June 20, 2016, 03:15:44 AM
Hi,

 It's because the character \ is understood differently when scripted in c# then when inputed within a  string variable in PlayMaker. It's known as an "escape" character which allow coders to insert custom characters easily.

http://www.codeproject.com/Articles/371232/Escaping-in-Csharp-characters-strings-string-forma


By doing a copy paste, you actually copy the real unicode reference of the "newline" character, not the litteral backslash.

Bye,

 Jean
Title: Re: An issue with a new line "\n" in string
Post by: playsteven on June 20, 2016, 04:19:45 AM
Thanks, Jean,

I find it invaluable to understand why something happens the way it does.

All the best,
Steven