Anonymous user
Scripting, Code & Interaction: Difference between revisions
→AGS Pointers for Dummies (A reference for the rest of us!)
Line 291: | Line 291: | ||
GUI *GUIPointer = gMygui; | GUI *GUIPointer = gMygui; | ||
Note that if the pointer is global (outside of all functions), then you cannot assign it an initial value. | '''''Note that if the pointer is global (outside of all functions), then you cannot assign it an initial value. | ||
Also note that if you are defining multiple pointers at once, you have to place an asterik before the name of each pointer. | Also note that if you are defining multiple pointers at once, you have to place an asterik before the name of each pointer.''''' | ||
Of course this would be a rather pointless assignment, unless you just wanted a new alias for your GUI. A more useful assignment makes use of the function GUI.GetAtScreenXY: | Of course this would be a rather pointless assignment, unless you just wanted a new alias for your GUI. A more useful assignment makes use of the function GUI.GetAtScreenXY: | ||
Line 312: | Line 312: | ||
First we assign the pointer to hold the value of the GUI under the mouse as we did before. Then we test whether it is null with the statement "if (GUIUnderMouse != null)" which reads as "GUIUnderMouse is not equal to null." If GUIUnderMouse was equal to null, then it wouldn't be pointing to anything, so we don't want to work with it. Next 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 whether it is null with the statement "if (GUIUnderMouse != null)" which reads as "GUIUnderMouse is not equal to null." If GUIUnderMouse was equal to null, then it wouldn't be pointing to anything, so we don't want to work with it. Next 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). | ||
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. 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, 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: | 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: | ||
Line 326: | Line 326: | ||
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*). | ||
Wait...what's with all this script o-name stuff? | ''Wait...what's with all this script o-name stuff?'' | ||
If you're reading this, you probably didn't ask that, so I asked it for you, because it pertains to this... | If you're reading this, you probably didn't ask that, so I asked it for you, because it pertains to this... | ||
Line 335: | Line 335: | ||
GUI *gInventory = gui[5]; | GUI *gInventory = gui[5]; | ||
It is a pointer to GUI #5, which is accessed globally by the gui array. Now for all I know the gui array could be just an array of pointers to something which is managed further inside the bowels of AGS, but it wouldn't really make a difference as they would still both be pointing to the same variable in the end. | It is a pointer to GUI #5, which is accessed globally by the '''gui''' array. Now for all I know the '''gui''' array could be just an array of pointers to something which is managed further inside the bowels of AGS, but it wouldn't really make a difference as they would still both be pointing to the same variable in the end. | ||
Okay, did you just say something about an array of pointers? | ''Okay, did you just say something about an array of pointers?'' | ||
Well, I'm not going to go into what an array is (look it up in the manual | Well, I'm not going to go into what an array is (look it up in the manual), but yes, you can create an array of pointers. You do this the same way you would create any other array. So, if you wanted to create your own array of pointers to the first five GUI objects, you could do something like this: | ||
GUI* MyGUIArray[5]; | GUI* MyGUIArray[5]; | ||
Line 350: | Line 350: | ||
} | } | ||
Note that if you have less than 5 GUIs in your game, this WILL crash it. | '''''Note that if you have less than 5 GUIs in your game, this WILL crash it.''''' | ||
I hope this helps everyone to understand pointers in AGS a little bit better. | '''*** NEW AS OF 3 JANUARY 2006 ***''' | ||
I forgot to mention, that you can even place a pointer inside of a struct. This can be another GREAT use for pointers in AGS. You define it the same way you define any other variable in a struct: | |||
struct StructNameHere { | |||
vartype1 var1; | |||
vartype2 var2; | |||
TypeToPointTo *PointerToType; | |||
}; | |||
Then, you use it like a normal pointer (just as a member of a variable of the new type). For example, suppose you want to store some new information about a character, say RPG statistics. You could use a pointer like this: | |||
struct CharStats { | |||
int HP; | |||
int MP; | |||
int MaxHP; | |||
int MaxMP; | |||
Character* Char; | |||
}; | |||
CharStats stEgo; | |||
// game_start | |||
stEgo.MaxHP = 100; | |||
stEgo.MaxMP = 50; | |||
stEgo.HP = 100; | |||
stEgo.MP = 50; | |||
stEgo.Char = cEgo; | |||
If you know how to use structs, then you will already understand what the integer values are for (if you don't check the manual), but what about this Character* (pointer to Character)? By storing this information, you can easily use '''stEgo''' ''in place of'' '''cEgo'''. Where you would normally type something like ''cEgo.Animate'', you would simply type ''stEgo.Char.Animate''. | |||
This is one of the most useful reasons for pointers as, to an extent, it can allow you to extend built-in types. | |||
And just to bring it all together, yes, you can even combine arrays, pointers, and structs all in one: | |||
struct CharStats { | |||
int HP[50]; | |||
int MP[50]; | |||
int MaxHP[50]; | |||
int MaxMP[50]; | |||
Character* Char[50]; | |||
}; | |||
Now then, I hope this helps everyone to understand pointers in AGS a little bit better. | |||
[[Category:AGS Beginners' FAQ]] | [[Category:AGS Beginners' FAQ]] |