Pointers can be quite a daunting prospect, and in languages like C and C++ they certainly
are; but AGS tries to make things as simple as possible.
Basically, a pointer is a variable that points to something of a particular type.
For example, a Character pointer would point to characters. What's the point of
all this, I hear you ask?
Well, let's look back at AGS 2.62. If you wanted to reference a particular hotspot,
you'd need to remember its number. If you wanted to switch on an object, you'd need to
remember what number it was too. And because you could accidentally use an object
number where you wanted a hotspot number, mistakes could easily happen and it all got
rather messy.
That's where pointers step in -- basically, they allow you to do away with identifying
things by number, and in the process provide type checking, so you can't accidentally
use a hotspot where you meant to use an object.
Let's look at an example. If you wanted to write a string to a file in 2.62, you'd do this:
int handle = FileOpen("temp.txt", FILE_WRITE);
FileWrite(handle, "Test!");
FileClose(handle);
That's simple enough; but what if you wanted to open the file in one place, and write
to it somewhere else? You'd have to make handle a global variable, and then make
sure you remembered that it was a file handle and not a hotspot number or anything else.
Now, with 2.7 the same code would be:
File *file = File.Open("temp.txt", eFileWrite);
file.WriteString("Test!");
file.Close();
Looks fairly simple, doesn't it. The only slightly confusing part is getting used
to declaring the variable as File* rather than int; but that's something
you'll get used to quite quickly, and all the examples in the manual should point
you in the right direction.
Let's look at another example. Suppose you want a variable that contains the current
hotspot that the mouse is over. In 2.62, you might have something like this:
// top of global script
int mouseOverHotspot;
// repeatedly_execute
mouseOverHotspot = GetHotspotAt(mouse.x, mouse.y);
How would you do this in 2.7? Well, quite simply:
// top of global script
Hotspot *mouseOverHotspot;
// repeatedly_execute
mouseOverHotspot = Hotspot.GetAtScreenXY(mouse.x, mouse.y);
But hold on, what if you want to know whether the mouse is over your Door hotspot (say
it's hotspot 2). In 2.62, you'd have done:
if (mouseOverHotspot == 2) {
Display("Mouse over the door");
}
but that's rather messy, because what if you change the door's hotspot number? You'd
have to remember to go back and change all the 2's to 3, or whatever. In 2.7, you now just
do this (assuming you gave the hotspot a script name of hDoor):
if (mouseOverHotspot == hDoor) {
Display("Mouse over the door");
}
If you're a fan of numbers for some strange reason, you can still use them like this:
if (mouseOverHotspot == hotspot[2]) {
Display("Mouse over the door");
}
So, that concludes our introduction to pointers. Hopefully you've got an understanding of
what they are and what they do; if there's anything you can't work out, feel free to ask
on the Technical forums.
|