Scripting, Code & Interaction: Difference between revisions

No edit summary
Line 289: Line 289:
However, you can work with these managed types through pointers.  You define a pointer by typing the variable type, i.e. GUI, 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, i.e. GUI, 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:


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.
Line 297: Line 297:
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:


GUI *GUIUnderMouse = GUI.GetAtScreenXY(mouse.x, mouse.y);
  GUI *GUIUnderMouse = GUI.GetAtScreenXY(mouse.x, mouse.y);


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.
Line 303: Line 303:
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 objects) 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:
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 objects) 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:


GUI *GUIUnderMouse = GUI.GetAtScreenXY(mouse.x, mouse.y);
  GUI *GUIUnderMouse = GUI.GetAtScreenXY(mouse.x, mouse.y);
if (GUIUnderMouse != null) {
  if (GUIUnderMouse != null) {
  if (GUIUnderMouse == gMygui) {
    if (GUIUnderMouse == gMygui) {
    Display("MYGUI is under the mouse!");
      Display("MYGUI is 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 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).
Line 318: Line 318:
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:


inventory[character[GetPlayerCharacter()].activeinv].Graphic = 42;
  inventory[character[GetPlayerCharacter()].activeinv].Graphic = 42;


However, in a system with pointers, you can type this instead:
However, in a system with pointers, you can type this instead:


player.ActiveInventory.Graphic = 42;
  player.ActiveInventory.Graphic = 42;


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*).
Line 332: Line 332:
Script o-names are essentially just pointers.  So, if you create GUI #5 and name it INVENTORY, then AGS automatically assigns it the script o-name, gInventory.  Basically gInventory will be defined internally as this:
Script o-names are essentially just pointers.  So, if you create GUI #5 and name it INVENTORY, then AGS automatically assigns it the script o-name, gInventory.  Basically gInventory will be defined internally as this:


// pseudo-internal-AGS-code
  // pseudo-internal-AGS-code
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.
Anonymous user