Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Monsieur OUXX on Sun 02/02/2014 09:18:28

Title: escape characters and '[' yet again...
Post by: Monsieur OUXX on Sun 02/02/2014 09:18:28
Hi all, i'm having big trouble managing the '[' character in my rendering.

Correct me if i'm wrong: (i've tested that myself by i'm always worried that i actually made a mistake in the test itself)
- in Labels, a '[' alone skips line
- in labels, '\[' displays a '[' (backslash acts as an escape char)
- in Labels, '\' displays '\'
- in Labels, '\\' displays '\\' (backslash doesn't act as an escape code on its own, neither several times in a row)
- in ListBoxes, '\' displays '\'
- in Listboxes, '[' displays '['

I didn't test any of that in "Display" but I assume it works like "Label.Text"

EDIT (see my new post later on):
- In Labels, "\\[" displays only "[". Same goes for "\\\[", or "\\\\[", etc. No matter how many backslahes.
Title: Re: escape characters and '[' yet again...
Post by: monkey0506 on Mon 03/02/2014 04:48:22
What exactly is your desired output?

You could use similarly named extender methods to specialize the returned output, effectively achieving a "addslashes"-style function if that's what you're after.
Title: Re: escape characters and '[' yet again...
Post by: Monsieur OUXX on Mon 03/02/2014 16:27:15
Quote from: monkey_05_06 on Mon 03/02/2014 04:48:22
You could use similarly named extender methods to specialize the returned output

That's what I'm currently doing, but in order to be sure of what I'm scripting, I need to be sure of the standard behaviour of each control (Label, Display, ListBox...). I think the differences that I've pointed out between Label and ListBox are correct.

I have bugs in my rendering but I cannot be more accurate here right now. I'll post again if they persist.

Title: Re: escape characters and '[' yet again...
Post by: Crimson Wizard on Mon 03/02/2014 16:32:12
Well, ListBoxes ignore '[' because they don't support multiline items.
Display supports '['.
Title: Re: escape characters and '[' yet again...
Post by: Monsieur OUXX on Mon 03/02/2014 21:17:22
OK there's somthing I really don't get.

Here is my code:



//returns the string "aaa\[bbb"
String GetTestString()
{
    real="aaa";
    real=real.AppendChar(eKeyBackSlash);
    real=real.AppendChar(eKeyOpenBracket);
    real=real.Append("bbb");
    return real;
}

//meant to render a string into a Label exactly the way it's stored in memory. Each character is rendered
void RenderStringReal(this Label*,  String str)
{
  String real="";
  int i=0;
  while (i<str.Length)
  {
    char c = str.Chars[ i];
    if (c==eKeyOpenBracket)
      real=real.AppendChar(eKeyBackSlash); //just a single backslash

    real=real.AppendChar(c);
    i++;
  }
  this.Text = real;
}

...
//at execution-time
myLabel.RenderStringReal(GetTestString());


No matter how many backslashes I insert
at the line "//just a single backslash", none of them is displayed. I only end up with the opening bracket. Does AGS swallow up all the backslashes preceding an opening bracket???
Title: Re: escape characters and '[' yet again...
Post by: Monsieur OUXX on Tue 04/02/2014 10:39:41
As monkey posted here (http://www.adventuregamestudio.co.uk/forums/index.php?topic=43094.msg572615#msg572615), "any other backslash is ignored". Managing backslashes is inconsistent. I think it's a bug and ought to be fixed:
- in the editor, a string like String s = "aaabb"; should raise an error at compiling time ("unfinished string" "unknown escape sequence")
- in a Label or Display, "aaa\\[bbb" should appear as:
aaa\[bbb
and not as:
aaa[bbb


Title: Re: escape characters and '[' yet again...
Post by: monkey0506 on Tue 04/02/2014 10:58:54
I can confirm that this is definitely a bug in the String implementation. Whenever any function manipulates the string (Append, AppendChar, Replace, etc.) (also, string literal -> String conversion) then the open bracket character ('[') will consume all backslashes ('\\') immediately preceding it. The backslashes are not consumed if there is any character between them and the bracket.

Quote from: Monsieur OUXX on Mon 03/02/2014 16:27:15
Quote from: monkey_05_06 on Mon 03/02/2014 04:48:22You could use similarly named extender methods to specialize the returned output

That's what I'm currently doing, but in order to be sure of what I'm scripting, I need to be sure of the standard behaviour of each control (Label, Display, ListBox...). I think the differences that I've pointed out between Label and ListBox are correct.

Obviously this particular bug is preventing you from doing it, but as an additional thought I just wanted to add that when I said "similarly named" that I didn't mean "almost similar" but rather "exactly similar", e.g.:

Code (ags) Select
import void AddSlashes(this Label*);
import void AddSlashes(this ListBox*);


That way you're invoking separate functions but with the same name, implying the same behavior is taking place. ;)




Quote from: Monsieur OUXX on Tue 04/02/2014 10:39:41- in the editor, a string like String s = "aaa\bbb"; should raise an error at compiling time ("unfinished string")

I actually disagree with this. While some compilers would vomit up "unsupported escape sequence" or some other such nonsense, I much prefer the regex approach where any non-escape sequence character can be used as an escape sequence representing itself (which is indeed the current approach taken by the AGS compiler).

Quote from: Monsieur OUXX on Tue 04/02/2014 10:39:41- in a Label or Display, "aaa\\\\\[bbb" should appear as:
aaa\\[bbb
and not as:
aaa[bbb

Agreed wholly on this, as per my statements above.
Title: Re: escape characters and '[' yet again...
Post by: Monsieur OUXX on Tue 04/02/2014 14:07:49
About the "\a": yes, as you pointed out, I meant that the error code should be "unknown escae sequence", definitely not "unfinished string".
Having said that, I'm in favor of an error message, you're in favor of letting it through. In any other laguage, both solutions would have their pros and cons.

But in the particular case of AGS, I think letting it through is not a good idea, considering the special role of "[". With that character, the escape "\" doesn't get swallowed up. With any other character, the "\" does get swallowed up. It's extremely confusing. And unlike \\ or \", that behaviour has no syntaxic role, it's only for anticipating a potential rendering later on.


PS:
Quote from: monkey_05_06 on Tue 04/02/2014 10:58:54
Whenever any function manipulates the string (Append, AppendChar, Replace, etc.) (also, string literal -> String conversion) then the open bracket character ('[') will consume all backslashes ('\\') immediately preceding it.
Wow, I thought the issue was only at rendering time, that explains why all my tests were failing. You just saved me from insanity!
Title: Re: escape characters and '[' yet again...
Post by: Monsieur OUXX on Tue 04/02/2014 15:21:29
Most of the questions raised in this discussion have been synthesised in this wiki page (http://www.adventuregamestudio.co.uk/wiki/Escaping_characters).
Title: Re: escape characters and '[' yet again...
Post by: Crimson Wizard on Tue 04/02/2014 17:20:14
Can you summarize the problem on the bug/suggestion tracker?
Title: Re: escape characters and '[' yet again...
Post by: monkey0506 on Tue 04/02/2014 20:14:21
Issue: Open bracket consuming too many backslashes in text output fields. (http://www.adventuregamestudio.co.uk/forums/index.php?issue=473.0)