AGS pointers work in a very similar way to object variables in Java and C#. The main
difference is that AGS pointers are declared in the C-style manner with an asterisk t
represent the pointer. So:
Hotspot *hs;
would declare a variable hs which points to a Hotspot. This would be equivalent to
the following in Java or C#:
Hotspot hs;
In AGS, pointers are used to point to various built-in types, such as Hotspots, Inventory Items,
Characters and so on. Because AGS does not have a new keyword, you cannot create pointers
to custom struct types.
You use pointers in the same way as you would in Java and C#. Various built-in AGS static
methods return a pointer to an instance (for example, File.Open,
Hotspot.GetAtScreenXY, and so on). You can save this
pointer into a pointer variable, and then call its methods as you would in Java or C#.
The following examples are all valid:
File *theFile = File.Open("test.dat", eFileWrite);
if (theFile == null) Display("It's null!");
File *file2 = theFile;
if (theFile == file2) Display("They're the same file!");
theFile = null;
file2.WriteInt(10);
file2.Close();
If you attempt to call a method on a null pointer, an error will occur (just like you'd
get an exception in Java or C#).
Pointer memory management in AGS is all automatic -- the memory is freed when there are
no longer any variables pointing to the instance. Thus, if you have global pointer variables
in your global script, it's a good idea to set them to null when you're no longer
using them, to allow the memory to be freed.
|