playMaker

Author Topic: An issue with a new line "\n" in string[SOLVED]  (Read 7478 times)

playsteven

  • Junior Playmaker
  • **
  • Posts: 75
    • View Profile
An issue with a new line "\n" in string[SOLVED]
« on: June 14, 2016, 06: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
« Last Edit: June 22, 2016, 10:57:32 PM by jeanfabre »

playsteven

  • Junior Playmaker
  • **
  • Posts: 75
    • View Profile
Re: An issue with a new line "\n" in string
« Reply #1 on: June 14, 2016, 06: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  :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15373
  • Official Playmaker Support
    • View Profile
Re: An issue with a new line "\n" in string
« Reply #2 on: June 20, 2016, 12: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

playsteven

  • Junior Playmaker
  • **
  • Posts: 75
    • View Profile
Re: An issue with a new line "\n" in string
« Reply #3 on: June 20, 2016, 01:19:45 AM »
Thanks, Jean,

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

All the best,
Steven