AGS Pointers for Dummies: Difference between revisions

*>Monkey 05 06
Line 61: Line 61:
First we assign the pointer to hold the value of the GUI under the mouse as we did before.  Then we test if the GUI is MYGUI with the statement "if (GUIUnderMouse == gMygui)".  If the GUI under the mouse was gMygui, then they will be equal, and the statement will pass as true and the statement will be displayed (avoid doing this repeatedly or else it could be a hassle to return back to the game).
First we assign the pointer to hold the value of the GUI under the mouse as we did before.  Then we test if the GUI is MYGUI with the statement "if (GUIUnderMouse == gMygui)".  If the GUI under the mouse was gMygui, then they will be equal, and the statement will pass as true and the statement will be displayed (avoid doing this repeatedly or else it could be a hassle to return back to the game).


==What pointers ''do''==
==What Pointers ''Do''==
''Okay, so we can create, assign, and test pointers, but what do they DO?''
''Okay, so we can create, assign, and test pointers, but what do they DO?''


Well, we've already discussed that they point to variables stored in the memory, but it's an interesting question as to how this can be useful. Let's take for example a built-in pointer, ''InventoryItem* Character.ActiveInventory''.  This is a pointer to an InventoryItem; the active inventory item for the Character who owns the property.
Well, we've already discussed that they point to variables stored in the memory, but it's an interesting question as to how this can be useful. Pointers can provide major advantages over systems which lack them, in several areas:


What it does is allows the user to operate on the active item without having to know it's integral value, or even what it is for that matter.  For example, if you wanted to change the item's graphic, with an integral system (with no pointers) you would have to do something like this:
===Pointer System Versus Integral System===
In a system with no pointers, you are likely to use a lot of integral references in referring to variables stored in the system. It's obvious how this could become difficult to work with, having to remember what number this character was, or what number that hotspot was, or what this integer flag you stored was. So let's compare some of the advantages:
 
====Character* and InventoryItem*====
Let's take for example a built-in pointer, ''InventoryItem* Character.ActiveInventory''.  This is a pointer to an InventoryItem; the active inventory item for the Character who owns the property.
 
What it does is allows the user to operate on the active item without having to know it's integral value.  For example, if you wanted to change the item's graphic, with an integral system (with no pointers) you would have to do something like this:


   inventory[character[GetPlayerCharacter()].activeinv].Graphic = 42;
   inventory[character[GetPlayerCharacter()].activeinv].Graphic = 42;
Line 75: Line 81:


So in addition to shortening the code, it also makes it easier to read.  The player keyword is a pointer to the player character (Character*) and ActiveInventory is a pointer to the player's active inventory item (InventoryItem*).
So in addition to shortening the code, it also makes it easier to read.  The player keyword is a pointer to the player character (Character*) and ActiveInventory is a pointer to the player's active inventory item (InventoryItem*).
====File*====
Another example can be seen if we look at the File type.


''Wait...what's with all this script o-name stuff?''
''Wait...what's with all this script o-name stuff?''
Anonymous user