Anonymous user
AGS Pointers for Dummies: Difference between revisions
no edit summary
*>Monkey 05 06 |
*>Monkey 05 06 No edit summary |
||
Line 85: | Line 85: | ||
=Null Pointers= | =Null Pointers= | ||
If a pointer isn't pointing to anything, it is known as a null pointer. It will actually hold the value '''null''' (which is equivalent to 0). Operations on null pointers will cause the game to crash, so you should always be sure that your pointer is non-null before using it. | If a pointer isn't pointing to anything, it is known as a null pointer. It will actually hold the value '''null''' (which is equivalent to 0)<sup>6</sup>. Operations on null pointers will cause the game to crash, so you should always be sure that your pointer is non-null before using it. | ||
You check if a pointer is null or not the same way you would normally check a pointer's value: | You check if a pointer is null or not the same way you would normally check a pointer's value: | ||
Line 91: | Line 91: | ||
if (GUIPointer == null) { /* the pointer is null */ } | if (GUIPointer == null) { /* the pointer is null */ } | ||
else { /* the pointer is non-null */ } | else { /* the pointer is non-null */ } | ||
<sup>6</sup>'''<font size="1">Though null is equivalent to zero, in most cases you should use the keyword null in place of its literal value.</font>''' | |||
=What Pointers ''Do''= | =What Pointers ''Do''= | ||
Line 112: | Line 114: | ||
Prior to AGS 2.71, AGS used the now deprecated string type. The string type was internally defined as an [http://bigbluecup.com/manual/Arrays.htm array] of 200 characters ('''char'''s). This meant that strings had a maximum length of 200 characters themselves. | Prior to AGS 2.71, AGS used the now deprecated string type. The string type was internally defined as an [http://bigbluecup.com/manual/Arrays.htm array] of 200 characters ('''char'''s). This meant that strings had a maximum length of 200 characters themselves. | ||
With the introduction of AGS 2.71 came the new String type which removed that limit. And how did it do it? It used a pointer. Not an AGS-style pointer, but a pointer nonetheless. In programming languages such as C and C++, a pointer-to-char (char*)<sup> | With the introduction of AGS 2.71 came the new String type which removed that limit. And how did it do it? It used a pointer. Not an AGS-style pointer, but a pointer nonetheless. In programming languages such as C and C++, a pointer-to-char (char*)<sup>7</sup> creates a special type of pointer. Instead of just pointing to one single variable, a char* can point to a virtually infinite number of chars in the form of what is known as a string-literal (such as "this is some text"). | ||
Since AGS uses a special type of pointer for managing the String type, it can still hold the value '''null''' (this is what Strings are initialized to), and when used as a function parameter, can be made optional in the same manner (see the section on [[#Optional_Pointer_Parameters|optional parameters]] for more information). | Since AGS uses a special type of pointer for managing the String type, it can still hold the value '''null''' (this is what Strings are initialized to), and when used as a function parameter, can be made optional in the same manner (see the section on [[#Optional_Pointer_Parameters|optional parameters]] for more information). | ||
<sup> | <sup>7</sup>'''<font size="1">In AGS you can't create a char*, as char isn't one of AGS's managed types. This type of pointer is used in scripting languages like C and C++. For storing string-literals AGS uses the String type (or the string type for AGS versions prior to 2.71).</font>''' | ||
===Script O-Names=== | ===Script O-Names=== | ||
Line 127: | Line 129: | ||
For all we know the gui array itself could be an array of pointers to something stored deeper within the bowels of AGS, but it's not really important as in the end they would still both point to the same GUI, and this is just an example anyway. | For all we know the gui array itself could be an array of pointers to something stored deeper within the bowels of AGS, but it's not really important as in the end they would still both point to the same GUI, and this is just an example anyway. | ||
Using an integral system you would have to acess the gui array any time you wanted to perform any operations on the GUI<sup> | Using an integral system you would have to acess the gui array any time you wanted to perform any operations on the GUI<sup>8</sup>. So, if we wanted to move MYGUI to (30, 120), in an integral system we could do this: | ||
gui[MYGUI].SetPosition(30, 120); | gui[MYGUI].SetPosition(30, 120); | ||
Line 137: | Line 139: | ||
So it makes our code a bit shorter then, but it's essentially the same. All-in-all not a particularlly convincing example. So let's take a look at another built-in pointer: '''player'''. | So it makes our code a bit shorter then, but it's essentially the same. All-in-all not a particularlly convincing example. So let's take a look at another built-in pointer: '''player'''. | ||
<sup> | <sup>8</sup>'''<font size="1">I have taken the liberty here of envisioning an integral system set up much as AGS 2.7+ is set up, only since it is an integral system it uses integers instead of pointers. In this example AGS structs still have member functions, and all other non-pointer-related functionality of AGS is the same.</font>''' | ||
===Player Keyword=== | ===Player Keyword=== | ||
Line 235: | Line 237: | ||
That would create a new datatype called MyStruct which would have two data members and one member function. You could then create a variable of this type, and do all sorts of fun things with it. Though it's uses don't end there. | That would create a new datatype called MyStruct which would have two data members and one member function. You could then create a variable of this type, and do all sorts of fun things with it. Though it's uses don't end there. | ||
You can also make a pointer a member of a struct<sup> | You can also make a pointer a member of a struct<sup>9</sup>, which provides some interesting possibilities. With a pointer as a member, you can essentially extend built-in datatypes (i.e., the managed types). | ||
<sup> | <sup>9</sup>'''<font size="1">Structs can only have pointers as members in AGS 2.71 and later.</font>''' | ||
===Extending the Character Type=== | ===Extending the Character Type=== |