1
\$\begingroup\$

I'm making something for a dialogue system that reads "cues" from a textfile to identify which emotion to show in the character's portrait during a conversation. Basically if the current line says "2", it means change the portrait to where the character is smiling. "1" for neutral, etc. Unfortunately the part that compares the emotion variable, and the string from the text file doesn't return true.

I already checked using debug.log if the string from the text file is showing the right number, and it does. I tried comparing them using "==" sign and ".equals();" but neither seems to work.

public TextAsset textFileP;
public string[] textLineP;   
public GameObject character;
public Animator anim;
public string smile = "2";

void Start()
{
    anim = GetComponent<Animator>();
   
    if (textFileP != null)
    {
        textLineP = (textFileP.text.Split('\n'));
        character.SetActive(true);
    }
}

void Update()
{
    
    if (DialogueParser.nextLine < DialogueParser.textLine.Length - 1)
    {
        
        //change portrait
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            string cue = textLineP[DialogueParser.nextLine];
            // Debug.Log(cue);
            if (cue.Equals(smile))
            {
                anim.SetInteger("2", 2);
            }

           
        }
      
\$\endgroup\$
9
  • 1
    \$\begingroup\$ Could there still be spaces around your cue after parsing it? Another possibility is, since smile is public that you set it to something different in the editor. To be sure, you could change your Debug.Log("-" + cue + "--" + smile + "-"); \$\endgroup\$
    – Zibelas
    Nov 30, 2020 at 13:11
  • \$\begingroup\$ Can you show us the file you are parsing? \$\endgroup\$
    – Philipp
    Nov 30, 2020 at 13:12
  • 3
    \$\begingroup\$ A common error here is assuming your lines are terminated only with \n when they might also include a \r. After splitting, that leaves you with strings with an invisible character at one end, enough to make them compare as not equal. You may want to use string methods like Trim() and ToLowerInvariant() to normalize your texts for comparison. \$\endgroup\$
    – DMGregory
    Nov 30, 2020 at 13:15
  • \$\begingroup\$ @Philipp it's just a .txt file that only contains single numbers line by line \$\endgroup\$ Nov 30, 2020 at 13:18
  • \$\begingroup\$ @loverofthefeet What program did you use to create that file? Notepad on Windows, perhaps? \$\endgroup\$
    – Philipp
    Nov 30, 2020 at 13:19

1 Answer 1

1
\$\begingroup\$

When you create your text file with most text editors on Windows, then lines aren't terminated just by the line-feed character (\n) but also by an additional carriage-return character before it (\r\n). So when you split such a text file by \n, then you will receive a bunch of strings suffixed by the invisible \r character.

You can actually see that in Notepad++ when you enable display of these symbols with View->Show Symbol->Show End Of Line. You will notice that there are not one but two invisible characters at the end of each line.

To fix that problem, split the file by "\r\n". Or if you want to make sure that it works even when your content creators use Linux (which uses only \n) or MacOS (which uses only \r), then you might either have to normalize your file content with .Replace, or you split the string in a way which accepts any flavor of line endings.

\$\endgroup\$
2
  • \$\begingroup\$ Adding "\r" on the trim made it work. Thank you! One last question, should I stick to this or should I try doing it on JSON, just like how you advised earlier? \$\endgroup\$ Nov 30, 2020 at 13:37
  • 1
    \$\begingroup\$ @loverofthefeet That depends on how complex you expect your file format to grow. When what you have now is all you need and you got a working solution, then why bother? But when you are going to add more features to it later, then you might want to switch to JSON (or another standardized markup language) before it gets too messy. (and before you created a ton of content which you would then have to convert to the new format). \$\endgroup\$
    – Philipp
    Nov 30, 2020 at 13:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .