Escaping characters: Difference between revisions

From Adventure Game Studio | Wiki
Jump to navigation Jump to search
(Created page with " When you manage your AGS Strings, you might want to pay special attention to those two characters: * '''"''' (double quote) * '''\''' (backslash) * '''[''' (opening square br...")
 
No edit summary
Line 8: Line 8:




 
{| class="wikitable"
'''How to read this walkthrough'''
|-
| '''How to read this walkthrough'''
There are 3 stages in managing your strings :  
There are 3 stages in managing your strings :  
1) How you type them into the Editor
* 1) How you type them into the '''Editor'''
2) How they will actually be stored into the engine's memory, at runtime
* 2) How they will actually be stored into the engine's '''memory''', at runtime
3) How they will be rendered into the display: in a Label, in a Listbox, or in a Display command
* 3) How they will be '''rendered''' into the display: in a ''Label'', in a ''Listbox'', or in a ''Display'' command.
|}


'''In the AGS script editor :'''  
<big>'''1) In the AGS script editor :'''</big>


(all the statements below tested in AGS 3.2.1)
(all the statements below tested in AGS 3.2.1)
Line 27: Line 29:
In the script editor, the backslash acts as an escape character, which means it has no value in itself, but depends on what comes immediately behind.
In the script editor, the backslash acts as an escape character, which means it has no value in itself, but depends on what comes immediately behind.


* One backslash followed by a double quote: \"
In editor:
{| class="wikitable"
{| class="wikitable"
|-
|-
| String s = "aaa\"bbb";
! Situation !! What you type into the script editor !! What you get in memory !! Explanations
|-
| One backslash followed by a double quote: '''\" ''' || String s = "aaa\"bbb"; || 97 97 97 34 98 98 98 || As you can see the backslash acts as an escape character and gets swallowed up so that the double quote doesn't get confused with the ending double quote.
 
By the way, this is '''not''' correct syntaxically, as there is no ending double quote:
String s = "aaa\";
 
|-
| One backslash followed by another backslash: '''\\''' || String s = "aaa\\bbb"; || 97 97 97 92 98 98 98 || As you can see the backslash acts as an escape character and gets swallowed up so that the second backslash actually makes it to the String in memory.
|-
| One backslash followed by an opening brace: '''\[''' || String s = "aaa\[bbb"; || 97 97 97 92 91 98 98 98 || As you can see the backslash acts as an escape character but DOES NOT get swallowed up. Both backslash and opening brace make it to memory.
This becomes useful at rendering time, to display a '''[''' in a Label or a Display.
|-
| One backslash followed by anything else: '''\a''', '''\b''', etc. || String s = "aaa\bbb"; || 97 97 97 98 98 98 || The backslash gets deleted.
It does not act as an escape character (the "a" immediately after it still makes it to memory) BUT still gets swallowed up, and doesn't appear in the in-memory string.
|-
| '''Any number of backslashes''' followed by something else  || String s = "aaa\\\[\bbb"; || 97 97 97 92 92 91 98 98 98 || All the above applies. Count your number of backslashes carefully!
The first two backslashes become just "\", "\[" stays "\[", "\b" becomes just "b".
Tricky, isn't it?
|}
|}
In memory:
 
'''Pro-tip :''' To avoid human mistakes, manipulate escape characters separately, using only their keyCode.
 
Example :  
{| class="wikitable"
{| class="wikitable"
|-
|-
| 97 97 97 34 98 98 98
! What you type in the script editor !! What you get in memory at run-time
|-
| String s=String.Format("%s%c%c%s", "aaa", eKeyBackSlash,  eKeyOpenBracket, "bbb"); || 97 97 97 92 91 98 98 98 <br>(aaa\[bbb)
|}
|}
As you can see the backslash acts as an escape character and gets swallowed up so that the double quote doesn't get confused with the ending double quote.


By the way, this is not correct syntaxically, as there is no ending double quote:
 
<big>'''2) In the GUI Editor'''</big>
 
* What you type in the "Text" property of the Label control is ''exactly''  what will be in memory. No escape characters apply.
* What will appear in the Label preview (in the GUI Editor, on the left) is rendered following the rules described in this page for Labels (read further).
 
{| class="wikitable"
{| class="wikitable"
|-
|-
| String s = "aaa\";
! What you type in the Text property !! What appears in the Label preview !! Explanation
|-
| aaa[bbb || aaa<br>bbb || See Label rendering rules.
|-
| aaa\[bbb || aaa[bbb || See Label rendering rules.
|-
| aaa\\\\[bbb || aaa[bbb || See Label rendering rules. All the backslashes get swallowed up at once!
|-
| aaa\bbb || aaa\bbb || (identical) See Label rendering rules.
|-
| aaa\\\\bbb || aaa\\\\bbb || (identical) See Label rendering rules.
|}
|}


'''At runtime, in a Label'''


* If followed by a backslash '''\''' : \\ allows you to store one "\" into memory at runtime, instead of having the backslash ignored (see below the sections about Labels, Listboxes, etc.)
<big>'''2) At runtime, in a Label or a Display()'''</big>
* Several backslashes followed by a brace '''[''' : '''\\\\[''' will only give you one [. All additional backslashes are ignored. Even the pairs of backslashes (\\) that you would expect to be turned into single \'s.
 
* If followed by a double quote '''"''' : That's the only way for you to insert a double quote into a String by simply typing it (otherwise you might want to use ''AppendChar(eKeyBackslash)''). The backslash will be ignored, and your double quote will be in the String, instead of causing an error at compiling time. The point about '''several''' backslashes preceding the double quote (explained earlier) also applies here.
 
{| class="wikitable"
|-
! The string as stored ''in memory'' !! How it will be rendered in a Label or Display !! Explanation
|-
| aaa[bbb || aaa<br>bbb || The opening brace is a special character that creates a new line.
|-
| aaa\[bbb || aaa[bbb || The brace's special behaviour is disabled if it's preceded by one or more backslashes. The backslashes are not rendered.
|-
| aaa\\\\[bbb || aaa[bbb || Please note that ''all'' the backslashes before a '[' don't get rendered.<br>Not only do they not get rendered, but they get '''deleted from the String''' (in-memory) if you make use of any of the following functions: '''Append''', '''AppendChar''', '''Replace''', etc. Also when you do a ''string'' -> ''String'' conversion (Reminder: ''string'' is the old-style AGS string, while ''String'' is the new-style AGS string since 3.x).<br>It doesn't seem to happen when you use ''String.Chars[i]''
|-
| aaa\bbb || aaa\bbb || The string gets rendered exactly as in memory, as the backslashes are not followed by '''['''.
|-
| aaa\\\\bbb || aaa\\\\bbb || The string gets rendered exactly as in memory, as the backslashes are not followed by '''['''.
|-
| aaa%bbb || ? || That's not correct. '%' is a special character meant for formatting Strings, as in ''String s = String.Format("The value of i is %d", i);''. Why would you try to render ''%b'' in a Label?
|-
| aaa%%bbb || aaa%bbb || A double percent in memory is rendered as a single percent on-screen
|}





Revision as of 14:33, 4 February 2014

When you manage your AGS Strings, you might want to pay special attention to those two characters:

  • " (double quote)
  • \ (backslash)
  • [ (opening square bracket, also known as left brace)

For easy solutions, simply read String_formatting#Displaying_special_characters. For thourough explanations, read below.


How to read this walkthrough

There are 3 stages in managing your strings :

  • 1) How you type them into the Editor
  • 2) How they will actually be stored into the engine's memory, at runtime
  • 3) How they will be rendered into the display: in a Label, in a Listbox, or in a Display command.

1) In the AGS script editor :

(all the statements below tested in AGS 3.2.1)

Let's say you want to declare a string containing a backslash (\) and or a double quote ("), in the following fashion :

String s = "this is a string, and soon it will contain a backslash or a double quote";

In the script editor, the backslash acts as an escape character, which means it has no value in itself, but depends on what comes immediately behind.

Situation What you type into the script editor What you get in memory Explanations
One backslash followed by a double quote: \" String s = "aaa\"bbb"; 97 97 97 34 98 98 98 As you can see the backslash acts as an escape character and gets swallowed up so that the double quote doesn't get confused with the ending double quote.

By the way, this is not correct syntaxically, as there is no ending double quote: String s = "aaa\";

One backslash followed by another backslash: \\ String s = "aaa\\bbb"; 97 97 97 92 98 98 98 As you can see the backslash acts as an escape character and gets swallowed up so that the second backslash actually makes it to the String in memory.
One backslash followed by an opening brace: \[ String s = "aaa\[bbb"; 97 97 97 92 91 98 98 98 As you can see the backslash acts as an escape character but DOES NOT get swallowed up. Both backslash and opening brace make it to memory.

This becomes useful at rendering time, to display a [ in a Label or a Display.

One backslash followed by anything else: \a, \b, etc. String s = "aaa\bbb"; 97 97 97 98 98 98 The backslash gets deleted.

It does not act as an escape character (the "a" immediately after it still makes it to memory) BUT still gets swallowed up, and doesn't appear in the in-memory string.

Any number of backslashes followed by something else String s = "aaa\\\[\bbb"; 97 97 97 92 92 91 98 98 98 All the above applies. Count your number of backslashes carefully!

The first two backslashes become just "\", "\[" stays "\[", "\b" becomes just "b". Tricky, isn't it?

Pro-tip : To avoid human mistakes, manipulate escape characters separately, using only their keyCode.

Example :

What you type in the script editor What you get in memory at run-time
String s=String.Format("%s%c%c%s", "aaa", eKeyBackSlash, eKeyOpenBracket, "bbb"); 97 97 97 92 91 98 98 98
(aaa\[bbb)


2) In the GUI Editor

  • What you type in the "Text" property of the Label control is exactly what will be in memory. No escape characters apply.
  • What will appear in the Label preview (in the GUI Editor, on the left) is rendered following the rules described in this page for Labels (read further).
What you type in the Text property What appears in the Label preview Explanation
aaa[bbb aaa
bbb
See Label rendering rules.
aaa\[bbb aaa[bbb See Label rendering rules.
aaa\\\\[bbb aaa[bbb See Label rendering rules. All the backslashes get swallowed up at once!
aaa\bbb aaa\bbb (identical) See Label rendering rules.
aaa\\\\bbb aaa\\\\bbb (identical) See Label rendering rules.


2) At runtime, in a Label or a Display()


The string as stored in memory How it will be rendered in a Label or Display Explanation
aaa[bbb aaa
bbb
The opening brace is a special character that creates a new line.
aaa\[bbb aaa[bbb The brace's special behaviour is disabled if it's preceded by one or more backslashes. The backslashes are not rendered.
aaa\\\\[bbb aaa[bbb Please note that all the backslashes before a '[' don't get rendered.
Not only do they not get rendered, but they get deleted from the String (in-memory) if you make use of any of the following functions: Append, AppendChar, Replace, etc. Also when you do a string -> String conversion (Reminder: string is the old-style AGS string, while String is the new-style AGS string since 3.x).
It doesn't seem to happen when you use String.Chars[i]
aaa\bbb aaa\bbb The string gets rendered exactly as in memory, as the backslashes are not followed by [.
aaa\\\\bbb aaa\\\\bbb The string gets rendered exactly as in memory, as the backslashes are not followed by [.
aaa%bbb ? That's not correct. '%' is a special character meant for formatting Strings, as in String s = String.Format("The value of i is %d", i);. Why would you try to render %b in a Label?
aaa%%bbb aaa%bbb A double percent in memory is rendered as a single percent on-screen


WARNING: This page is still being redacted.