Search Unity

Multi-line strings?

Discussion in 'Scripting' started by Sir-Magic, Feb 8, 2016.

  1. Sir-Magic

    Sir-Magic

    Joined:
    Jul 20, 2015
    Posts:
    50
    Hi, I am making a game that realies heavilly on narrative and I believe it to be extremely tedious to have to write many quotaion marks for longer strings. In python, you could use multi-line strings like this:
    """
    Insert multi-line quote
    here

    """"
    Surely, there has to be a way to perform this in javascript. In case you need to know, I use notepad++ as my text editor. All help appreciated.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There isn't a way. Use "\n" for a newline character.

    Code (javascript):
    1. var text = "Insert multi-line quote\nhere";
    --Eric
     
    Sir-Magic likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You may want to consider a data-driven approach: put the text you want into a file (or files) on the disk, and then use it as a Unity3d TextAsset object, which you can drag-by-reference into a script in the editor window.

    Then you can access the text strings in code and display them that way.
     
    karl_jones likes this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That won't actually work; it is a requirement that a string is all on one line. (You can concatenate over multiple lines, but the OP didn't want to do that.)

    --Eric
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    You're completely right, that was broken.
     
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    EDIT: What I wrote does not work in UnityScript. I'm too used to C# >.>

    Code (csharp):
    1. string worksExceptDoubleQuote = @"This is
    2. a way you can have strings
    3. across multiple lines.
    4.  
    5. Unless it's a "", then you need to escape
    6. with another "" in front of it.";
    7.  
    8. // This is
    9. // a way you can have strings
    10. // across multiple lines.
    11. //
    12. // Unless it's a ", then you need to escape
    13. // with another " in front of it.
    I like putting text in files so I don't have to worry about escaping anything.

    (Apparently the forum doesn't know how to properly color that code!)
     
    Last edited: Feb 9, 2016