AGS Pointers for Dummies: Difference between revisions

Jump to navigation Jump to search
*>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)<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.
If a pointer isn't pointing to anything, it is known as a null pointer. It will actually hold the value '''null'''. 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 114: Line 112:
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>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").
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>6</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>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>'''
<sup>6</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 129: Line 127:
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>8</sup>. So, if we wanted to move MYGUI to (30, 120), in an integral system we could do this:
Using an integral system you would have to acess the gui array any time you wanted to perform any operations on the GUI<sup>7</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 139: Line 137:
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>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>'''
<sup>7</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 215: Line 213:
''Okay, that's nice, but how does it apply to pointers? I tried assigning my Character* parameter a default value and it didn't work.''
''Okay, that's nice, but how does it apply to pointers? I tried assigning my Character* parameter a default value and it didn't work.''


AGS doesn't currently allow you to assign non-integer default values. This means that to make a parameter optional we will have to give it an integer value. But what integer value can we use with pointers? Remember how I mentioned before that '''null''' is equivalent to 0? In most cases you should use null to check for null pointers instead of using 0, however, you can't assign a pointer a default value of null. You ''can'' however set a default value of 0.
AGS doesn't currently allow you to assign non-integer default values. This means that to make a parameter optional we will have to give it an integer value. But what integer value can we use with pointers? Though it is not normally recommended (and in most cases won't work), we can ''substitute'' the value 0 for null in this case.


This does of course mean that you would have to have some means of handling null values for that parameter. Perhaps, for a Character*, it could default to the player character. You could do this by checking your parameter, and if it is null, reassigning it to the player character:
This does of course mean that you would have to have some means of handling null values for that parameter. Perhaps, for a Character*, it could default to the player character. You could do this by checking your parameter, and if it is null, reassigning it to the player character:
Line 237: Line 235:
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>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).
You can also make a pointer a member of a struct<sup>8</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>9</sup>'''<font size="1">Structs can only have pointers as members in AGS 2.71 and later.</font>'''
<sup>8</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===
Anonymous user

Navigation menu