AGS Pointers for Dummies: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 1: Line 1:
'''AGS Pointers for Dummies (A reference for the rest of us!)'''
=AGS Pointers for Dummies (A reference for the rest of us!)=


So you're new to Adventure Game Studio (at least new to version 2.7 or later) and you're a bit confused about all this "pointer" business? Don't worry, you're not alone. A lot of people are confused about pointers. That's why I'm here to help.
So you're new to Adventure Game Studio (at least new to version 2.7 or later) and you're a bit confused about all this "pointer" business? Or maybe you've read the manual, but you just don't get it? Don't worry, you're not alone. A lot of people are confused about pointers. That's why I'm here to help.


==What are pointers?==
==What Are Pointers?==
''So what exactly are pointers, and how do I use them?''
''So what exactly are pointers?''


The basic idea of a pointer is that instead of creating a new variable in the memory, you are just going to ''point'' to one that is already stored there. This can have several uses, and in AGS even has some special ones.
The basic idea of a pointer is that instead of creating a new variable in the memory, you are just going to ''point'' to one that is already stored there. This can have several uses, and in AGS even has some special ones.


===Managed Types in AGS===
==Defining A Pointer==
''And, how do I use them?''
 
In AGS, you can only create pointers to certain types of variables. These are called ''managed'' types.
 
===Managed Types===
AGS has certain ''managed'' types that you can not create an instance (variable declaration) of.  All of the variables of managed types are ''managed'' by AGS.  These include the types:  [http://www.bigbluecup.com/manual/Room%20_%20Screen%20functions.htm Room], [http://www.bigbluecup.com/manual/Game%20_%20Global%20functions.htm Game], [http://www.bigbluecup.com/manual/Parser%20functions.htm Parser], [http://www.bigbluecup.com/manual/File%20functions%20and%20properties.htm File], [http://www.bigbluecup.com/manual/Inventory%20item%20functions%20and%20properties.htm InventoryItem], [http://www.bigbluecup.com/manual/Overlay%20functions%20and%20properties.htm Overlay], [http://www.bigbluecup.com/manual/DynamicSprite%20functions%20and%20properties.htm DynamicSprite], [http://www.bigbluecup.com/manual/GUIFuncsAndProps.htm GUI], [http://www.bigbluecup.com/manual/GUI%20Label%20functions%20and%20properties.htm Label], [http://www.bigbluecup.com/manual/GUI%20Button%20functions%20and%20properties.htm Button], [http://www.bigbluecup.com/manual/GUI%20Slider%20properties.htm Slider], [http://www.bigbluecup.com/manual/GUI%20Text%20Box%20functions%20and%20properties.htm TextBox], [http://www.bigbluecup.com/manual/GUIInvFuncs.htm InvWindow], [http://www.bigbluecup.com/manual/GUI%20List%20Box%20functions%20and%20properties.htm ListBox], [http://www.bigbluecup.com/manual/Character%20functions%20and%20properties.htm Character], [http://www.bigbluecup.com/manual/GUI%20control%20functions%20and%20properties.htm GUIControl], [http://www.bigbluecup.com/manual/Hotspot%20functions%20and%20properties.htm Hotspot], [http://www.bigbluecup.com/manual/Region%20functions%20and%20properties.htm Region], [http://www.bigbluecup.com/manual/Maths%20functions%20and%20properties.htm Maths], [http://www.bigbluecup.com/manual/DateTime%20functions%20and%20properties.htm DateTime], and [http://www.bigbluecup.com/manual/Object%20functions%20and%20properties.htm Object].
AGS has certain ''managed'' types that you can not create an instance (variable declaration) of.  All of the variables of managed types are ''managed'' by AGS.  These include the types:  [http://www.bigbluecup.com/manual/Room%20_%20Screen%20functions.htm Room], [http://www.bigbluecup.com/manual/Game%20_%20Global%20functions.htm Game], [http://www.bigbluecup.com/manual/Parser%20functions.htm Parser], [http://www.bigbluecup.com/manual/File%20functions%20and%20properties.htm File], [http://www.bigbluecup.com/manual/Inventory%20item%20functions%20and%20properties.htm InventoryItem], [http://www.bigbluecup.com/manual/Overlay%20functions%20and%20properties.htm Overlay], [http://www.bigbluecup.com/manual/DynamicSprite%20functions%20and%20properties.htm DynamicSprite], [http://www.bigbluecup.com/manual/GUIFuncsAndProps.htm GUI], [http://www.bigbluecup.com/manual/GUI%20Label%20functions%20and%20properties.htm Label], [http://www.bigbluecup.com/manual/GUI%20Button%20functions%20and%20properties.htm Button], [http://www.bigbluecup.com/manual/GUI%20Slider%20properties.htm Slider], [http://www.bigbluecup.com/manual/GUI%20Text%20Box%20functions%20and%20properties.htm TextBox], [http://www.bigbluecup.com/manual/GUIInvFuncs.htm InvWindow], [http://www.bigbluecup.com/manual/GUI%20List%20Box%20functions%20and%20properties.htm ListBox], [http://www.bigbluecup.com/manual/Character%20functions%20and%20properties.htm Character], [http://www.bigbluecup.com/manual/GUI%20control%20functions%20and%20properties.htm GUIControl], [http://www.bigbluecup.com/manual/Hotspot%20functions%20and%20properties.htm Hotspot], [http://www.bigbluecup.com/manual/Region%20functions%20and%20properties.htm Region], [http://www.bigbluecup.com/manual/Maths%20functions%20and%20properties.htm Maths], [http://www.bigbluecup.com/manual/DateTime%20functions%20and%20properties.htm DateTime], and [http://www.bigbluecup.com/manual/Object%20functions%20and%20properties.htm Object].


'''''Note that not all of the managed types are meant to have pointers to them.  Room, Game, Parser, and Maths do not need pointers (you can't even assign them a value).'''''
'''''Note that not all of the managed types are meant to have pointers to them.  Room, Game, Parser, and Maths do not need pointers (you can't even assign them a value).'''''


===Defining a pointer===
===Working With Managed Types===
However, you can work with these managed types through pointers.  You define a pointer by typing the variable type, then a space, an asterik (*), and finally the name of the pointer.  So, to create a pointer named GUIPointer to point to the GUI object named gMygui, you could type:
However, you can work with these managed types through pointers.  You define a pointer by typing the variable type, then a space, an asterik (*), and finally the name of the pointer.  So, to create a pointer named GUIPointer to point to the GUI object named gMygui, you could type:


Line 24: Line 29:
'''''Furthermore, you should take note that the asterik can actually be placed beside the variable type (i.e., GUI* GUIPointer instead of GUI *GUIPointer), but it will always be parsed as being attached to the variable (i.e., GUI *GUIPointer).'''''
'''''Furthermore, you should take note that the asterik can actually be placed beside the variable type (i.e., GUI* GUIPointer instead of GUI *GUIPointer), but it will always be parsed as being attached to the variable (i.e., GUI *GUIPointer).'''''


====A More Useful Assignment====
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 30: Line 36:
As implied by the name of the pointer, this pointer will point to the GUI that the mouse is over.  This brings to point an interesting question though.  What if there is no GUI under the mouse?  Well, in that case, GUIUnderMouse would be set to ''null'', which means that it isn't pointing to anything.
As implied by the name of the pointer, this pointer will point to the GUI that the mouse is over.  This brings to point an interesting question though.  What if there is no GUI under the mouse?  Well, in that case, GUIUnderMouse would be set to ''null'', which means that it isn't pointing to anything.


If a pointer is null, then basically all you can do with the pointer is make it point to something (assign it a value as in the examples), and test it against other pointers (or variables) of the same type. We've already seen how to assign a value to a pointer, so let's see how we can compare two pointers.  Let's take the following example:
==Null Pointers==
If a pointer is null, then basically all you can do with the pointer is make it point to something (assign it a value as in the examples), and test it against other pointers (or variables) of the same type. You can test to see if a variable is null with the '==' operator.
 
Suppose, for example, we have a GUI pointer (GUI*) called GUIUnderMouse which we will use to point to the GUI under the mouse. We can check if there is a GUI under the mouse (after assignment) with the following line:
 
  GUI* GUIUnderMouse = GUI.GetAtScreenXY(mouse.x, mouse.y) // assignment
  if (GUIUnderMouse == null) {} // no GUI under mouse
 
You can also test if a pointer is ''not'' equal to null with the '!=' operator:
 
  if (GUIUnderMouse != null) {} // there is a GUI under the mouse
 
==Comparing Pointers==
We've seen how to assign values to pointers, and how to test whether or not they are null, so let's take a look at comparing them to other pointers of the same type:


   GUI *GUIUnderMouse = GUI.GetAtScreenXY(mouse.x, mouse.y);
   GUI *GUIUnderMouse = GUI.GetAtScreenXY(mouse.x, mouse.y);
   if (GUIUnderMouse != null) {
   if (GUIUnderMouse == gMygui) {
     if (GUIUnderMouse == gMygui) {
     Display("MYGUI is under the mouse!");
      Display("MYGUI is under the mouse!");
    }
      }
  else if (GUIUnderMouse != gMygui) {
    Display("MYGUI is not under the mouse!");
     }
     }


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 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''==
''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?''


Anonymous user

Navigation menu