Anonymous user
AGS Pointers for Dummies: Difference between revisions
Jump to navigation
Jump to search
→What Pointers ''Do''
Line 111: | Line 111: | ||
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"). | 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_Parameters|optional parameters]] for more information). | |||
<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>''' | <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>''' | ||
Line 191: | Line 193: | ||
You create a File* which points to the opened file. You still have to close the file using the File*, but it's simpler since you are using a specifically created File* instead of just a generic int variable. | You create a File* which points to the opened file. You still have to close the file using the File*, but it's simpler since you are using a specifically created File* instead of just a generic int variable. | ||
===Pointers as Function Parameters=== | |||
Pointers can also be used for function parameters. Those who have used versions of AGS prior to 2.7 know that integers used to be passed as parameters for several functions which have now been made into OO ([http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented]) functions, such as MoveCharacter (now known as Character.Move). | |||
The old MoveCharacter function took three parameters: CHARID, int x, and int y. CHARID was an integer parameter which held the character's ID (this is the same as the new Character.ID property). But what if we had the MoveCharacter function in a pointer-implemented, non-OO system? The parameter list would probably be something like this: Character *Char, int x, int y. | |||
The first parameter, a Character* would allow us to pass a Character* instead of just an int, which helps make clearer what the code is trying to accomplish. It also ensures that the parameter is valid (to an extent). An integer parameter could have any value passed into it, which the function would then have to check. A Character* helps to ensure the value is valid, though since it is a pointer it could still be null. | |||
====Optional Parameters==== | |||
As of AGS 2.7 you can make function parameters optional by assigning them a default value when you import the function. For example, to make a function with an optional int parameter, you can define the import like this: | |||
import myfunc(int param1, int param2=5); | |||
That would make PARAM2 optional, with the default value of 5. This import doesn't necessarily have to be placed in a script header (which is where most of your imports will be). If you don't want the function to be globally accessible but you want an optional parameter, you can just put this import in your script before you define the function and it will allow you to have a non-global function with an optional parameter. | |||
''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 always 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. | |||
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: | |||
if (Char == null) Char = player; | |||
Also, you may remember my mentioning that the String type uses pointers? You can make a String parameter optional in the same way you make any other pointer parameter optional, by assigning it the value of 0. This will cause the parameter to default to a null String. | |||
==Extending The Built-In (Managed) Types== | ==Extending The Built-In (Managed) Types== | ||
Line 207: | Line 233: | ||
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). | 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>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=== | ||
Line 232: | Line 260: | ||
You can extend any of the managed types that you can create pointers to in this manner. | You can extend any of the managed types that you can create pointers to in this manner. | ||
=Closing= | =Closing= |