Dusk412
07-24-2008, 08:38 PM
http://i235.photobucket.com/albums/ee171/Dusk412/TPAsforDUMMIES.png
Table Of Contents:
- Background Information
- Basic Use Of TPAs
- Advanced Use Of TPAs (http://www.villavu.com/forum/showthread.php?t=33111?p=436802#post436802)
- Using T2DPointArrays (http://www.villavu.com/forum/showthread.php?t=33111?p=436803#post436803)
- Conclusion
Background Information:
What is a TPA, you ask?
- Well, TPA stands for “TPointArray” Now that may sound like complex programmer language to you, but the truth is that TPAs are a fairly simple and powerful tool used frequently on SRL Forums. Before we can begin to explain exactly what and how to utilize this tool, we must first understand what an array is.
Well then, what is an array?
- An array is, in simple terms, a shorthand way of declaring a whole lot of variables of the same type. Some of the more frequently used types are integers, booleans, and strings. For the sake of understanding, here is an example.
- Example: In a classroom, you are a student. In this class you have tons of grades. If you were to write a program to keep track of grades for you, you would need a variable for each and every grade you had. Say you had just 10 grades. In this program all we want to do is write each of the grades to the Debug box (the box in the corner of SCAR).
program TPATutorial;
{.include SRL/SRL.scar}
var
Grade1, Grade2, Grade3, Grade4, Grade5, Grade6, Grade7, Grade8, Grade9, Grade10 : Integer;
begin
Writeln(inttostr(Grade1));
Writeln(inttostr(Grade2));
Writeln(inttostr(Grade3));
Writeln(inttostr(Grade4));
Writeln(inttostr(Grade5));
Writeln(inttostr(Grade6));
Writeln(inttostr(Grade7));
Writeln(inttostr(Grade8));
Writeln(inttostr(Grade9));
Writeln(inttostr(Grade10));
end.
- As you can see this is a fairly long program for the simplest of tasks. We must declare each variable separately, and then call on each variable separately. Using arrays, we can shorten this to just a few lines.
program TPATutorial;
{.include SRL/SRL.scar}
var
Grade: array [0..9] of Integer;
i : Integer;
begin
for i := 0 to 9 do
begin
Writeln(inttostr(Grade[i]));
end;
end.
- In this much shorter example, we can declare all 10 grade variables at once. You could call on them individually by using Grade[0], Grade[1], … Grade[9], or we can use the much faster method of using a for statement that runs the integer I from 0 to 9, therefore calling on each grade in turn. If you wanted to do this for 100 or even 1000 grades, it would hardly lengthen at all, whereas the other function would gain hundreds more lines.
- Now that we understand what an array is, you are probably asking yourself, how do we have an array of a TPoint? In fact, what on earth is a TPoint at all? Which brings me to my next point.
What the heck is a TPoint?
- I do not wish to go and totally cover basic geometry, so I will quickly summarize. On the Cartesian Plane (the graph with the x-axis and the y-axis), you can describe any one spot by using a combination of two numbers, the x and y coordinates, shown like this:
(x, y)
(0,0)
(12, 3)
- A combination of one x and one y coordinate describes exactly one spot, called a point. In SRL, we call points TPoints. Therefore a TPoint is really two values, an x and a y. You can store values to a TPoint like this:
program TPATutorial;
{.include SRL/SRL.scar}
var
MyPoint : TPoint;
begin
MyPoint:= inttopoint(5,7);
end.
- the function ‘inttopoint’ takes two integers, in this case x and y, and converts them to one TPoint. You can also store values and call on values of TPoints like this:
program TPATutorial;
{.include SRL/SRL.scar}
var
MyPoint : TPoint;
begin
MyPoint.x := 5;
MyPoint.y := 7;
Writeln(inttostr(MyPoint.x));
end.
- Using the name of the TPoint, followed by a period, followed by either x or y, depending on which value you want. So now you understand arrays and TPoints, and you want to know why you could possible need to have a whole array of these silly points.
Why do we need TPointArrays?
- Say you have a square, each corner on said square has a coordinate. Now what you could do, as I explained with arrays, is make a variable TPoint for each and every corner. With a square this may seem simple, but what if you want an octagon, what about if you want to get every corner on the outline of a runescape object, such as a fish or a bucket. These objects have tens if not hundreds of points on them and it would take tons of memory and space to create a program to use them. But through the use of TPointArrays, you can store them all in one variable and utilize and manipulate them to your advantage.
Basic Use Of TPAs:
- The very first thing you must do, to get a TPA, is declare it. There are a few ways in which to go about this. The first would be to do it as if creating an array.
MyTPA : array of TPoint;
MyTPA : array [0..9] of TPoint;
- But the developers of SCAR and SRL have gone beyond this and create a whole new type, just for TPAs called TPointArray (of course)! So the best, coolest, and newest way to do it would be like so:
MyTPA : TPointArray;
- But one thing you may have noticed that is missing, is the length of the array, that is, how many different TPoints we can store. If you do not know how long the TPA is going to need to be at the very beginning of the script, you may simply leave the length until later. If you know exactly how long it needs to be, you may declare that at the beginning. There are two procedures that do the exact same thing.
procedure SetLength(var s; NewLength: Integer);
procedure SetArrayLength(var Arr; Count: Integer);
- Both functions take the TPointArray stored in either variable s or Arr (depending on the procedure) and change the length to either NewLength or Count (again, depending on which procedure). You use it like this:
SetLength(MyTPA, 10);
SetArrayLength(MyTPA, 10);
- Both of these examples set the length TPA, MyTPA, to 10. Meaning there are 10 different values that can be stored, from MyTPA[0] to MyTPA[9];
- Note: TPAs always start at 0, not 1! So if you want 10 values, the highest TPoint will be 9, if you want 100 values, the highest TPoint will be 99, etc.
- The reason that you will not always need to declare a length at the beginning of a script is that many of the functions and procedures that are most common and most useful in SRL and SCAR are the ones that will find all the locations that meet a specific requirement, such as color, and will store however many values there are. That is why these next two functions where created.
function Length(s): Integer;
function GetArrayLength(var Arr): Integer;
- These functions take the TPA stored under s or Arr and return the length of the TPA, or the number of values stored in it, as an integer;
program TPATutorial;
{.include SRL/SRL.scar}
var
TPALength : Integer;
MyTPA : TPointArray;
begin
TPALength := Length(MyTPA);
Writeln(inttostr(TPALength));
TPALength := GetArrayLength(MyTPA);
Writeln(inttostr(TPALength));
end.
- In this example I got the length of the TPA, MyTPA, and stored it in the variable TPALength, an integer. This is, as I stated earlier, useful when finding points and storing them all when you do not know how many different points you will get. You may then run through these points and check for something, like a specific uptext, for example. Some powerful, yet extremely simple, functions when manipulating TPAs are the following:
function High(x): Int64;
- By putting the TPA in for x, it will return the highest possible index for our TPA. The index is the number that goes in the brackets after the TPA name. MyTPA[2] has and index of 2. MyTPA[i] has an index of i. So if we have a TPA of length 10 (this means our TPA goes from 0 to 9, remember), High would return 9.
function Low(x): Int64;
- Similarly, Low will return the lowest possible index for our TPA. So if we have a TPA of length 10 that goes from 0 to 9, it will return 0. This is not actually particularly useful for TPAs, it is generally only used on static arrays. I just thought I would mention it. (Credits to mixster for helping me figure out what the use of Low was for :D).
program TPATutorial;
{.include SRL/SRL.scar}
var
MyTPA : TPointArray;
begin
SetLength(MyTPA, 10);
Writeln('High = '+inttostr(High(MyTPA)));
Writeln('Low = '+inttostr(Low(MyTPA)));
end.
- There is an example of how the functions High and Low work.
Table Of Contents:
- Background Information
- Basic Use Of TPAs
- Advanced Use Of TPAs (http://www.villavu.com/forum/showthread.php?t=33111?p=436802#post436802)
- Using T2DPointArrays (http://www.villavu.com/forum/showthread.php?t=33111?p=436803#post436803)
- Conclusion
Background Information:
What is a TPA, you ask?
- Well, TPA stands for “TPointArray” Now that may sound like complex programmer language to you, but the truth is that TPAs are a fairly simple and powerful tool used frequently on SRL Forums. Before we can begin to explain exactly what and how to utilize this tool, we must first understand what an array is.
Well then, what is an array?
- An array is, in simple terms, a shorthand way of declaring a whole lot of variables of the same type. Some of the more frequently used types are integers, booleans, and strings. For the sake of understanding, here is an example.
- Example: In a classroom, you are a student. In this class you have tons of grades. If you were to write a program to keep track of grades for you, you would need a variable for each and every grade you had. Say you had just 10 grades. In this program all we want to do is write each of the grades to the Debug box (the box in the corner of SCAR).
program TPATutorial;
{.include SRL/SRL.scar}
var
Grade1, Grade2, Grade3, Grade4, Grade5, Grade6, Grade7, Grade8, Grade9, Grade10 : Integer;
begin
Writeln(inttostr(Grade1));
Writeln(inttostr(Grade2));
Writeln(inttostr(Grade3));
Writeln(inttostr(Grade4));
Writeln(inttostr(Grade5));
Writeln(inttostr(Grade6));
Writeln(inttostr(Grade7));
Writeln(inttostr(Grade8));
Writeln(inttostr(Grade9));
Writeln(inttostr(Grade10));
end.
- As you can see this is a fairly long program for the simplest of tasks. We must declare each variable separately, and then call on each variable separately. Using arrays, we can shorten this to just a few lines.
program TPATutorial;
{.include SRL/SRL.scar}
var
Grade: array [0..9] of Integer;
i : Integer;
begin
for i := 0 to 9 do
begin
Writeln(inttostr(Grade[i]));
end;
end.
- In this much shorter example, we can declare all 10 grade variables at once. You could call on them individually by using Grade[0], Grade[1], … Grade[9], or we can use the much faster method of using a for statement that runs the integer I from 0 to 9, therefore calling on each grade in turn. If you wanted to do this for 100 or even 1000 grades, it would hardly lengthen at all, whereas the other function would gain hundreds more lines.
- Now that we understand what an array is, you are probably asking yourself, how do we have an array of a TPoint? In fact, what on earth is a TPoint at all? Which brings me to my next point.
What the heck is a TPoint?
- I do not wish to go and totally cover basic geometry, so I will quickly summarize. On the Cartesian Plane (the graph with the x-axis and the y-axis), you can describe any one spot by using a combination of two numbers, the x and y coordinates, shown like this:
(x, y)
(0,0)
(12, 3)
- A combination of one x and one y coordinate describes exactly one spot, called a point. In SRL, we call points TPoints. Therefore a TPoint is really two values, an x and a y. You can store values to a TPoint like this:
program TPATutorial;
{.include SRL/SRL.scar}
var
MyPoint : TPoint;
begin
MyPoint:= inttopoint(5,7);
end.
- the function ‘inttopoint’ takes two integers, in this case x and y, and converts them to one TPoint. You can also store values and call on values of TPoints like this:
program TPATutorial;
{.include SRL/SRL.scar}
var
MyPoint : TPoint;
begin
MyPoint.x := 5;
MyPoint.y := 7;
Writeln(inttostr(MyPoint.x));
end.
- Using the name of the TPoint, followed by a period, followed by either x or y, depending on which value you want. So now you understand arrays and TPoints, and you want to know why you could possible need to have a whole array of these silly points.
Why do we need TPointArrays?
- Say you have a square, each corner on said square has a coordinate. Now what you could do, as I explained with arrays, is make a variable TPoint for each and every corner. With a square this may seem simple, but what if you want an octagon, what about if you want to get every corner on the outline of a runescape object, such as a fish or a bucket. These objects have tens if not hundreds of points on them and it would take tons of memory and space to create a program to use them. But through the use of TPointArrays, you can store them all in one variable and utilize and manipulate them to your advantage.
Basic Use Of TPAs:
- The very first thing you must do, to get a TPA, is declare it. There are a few ways in which to go about this. The first would be to do it as if creating an array.
MyTPA : array of TPoint;
MyTPA : array [0..9] of TPoint;
- But the developers of SCAR and SRL have gone beyond this and create a whole new type, just for TPAs called TPointArray (of course)! So the best, coolest, and newest way to do it would be like so:
MyTPA : TPointArray;
- But one thing you may have noticed that is missing, is the length of the array, that is, how many different TPoints we can store. If you do not know how long the TPA is going to need to be at the very beginning of the script, you may simply leave the length until later. If you know exactly how long it needs to be, you may declare that at the beginning. There are two procedures that do the exact same thing.
procedure SetLength(var s; NewLength: Integer);
procedure SetArrayLength(var Arr; Count: Integer);
- Both functions take the TPointArray stored in either variable s or Arr (depending on the procedure) and change the length to either NewLength or Count (again, depending on which procedure). You use it like this:
SetLength(MyTPA, 10);
SetArrayLength(MyTPA, 10);
- Both of these examples set the length TPA, MyTPA, to 10. Meaning there are 10 different values that can be stored, from MyTPA[0] to MyTPA[9];
- Note: TPAs always start at 0, not 1! So if you want 10 values, the highest TPoint will be 9, if you want 100 values, the highest TPoint will be 99, etc.
- The reason that you will not always need to declare a length at the beginning of a script is that many of the functions and procedures that are most common and most useful in SRL and SCAR are the ones that will find all the locations that meet a specific requirement, such as color, and will store however many values there are. That is why these next two functions where created.
function Length(s): Integer;
function GetArrayLength(var Arr): Integer;
- These functions take the TPA stored under s or Arr and return the length of the TPA, or the number of values stored in it, as an integer;
program TPATutorial;
{.include SRL/SRL.scar}
var
TPALength : Integer;
MyTPA : TPointArray;
begin
TPALength := Length(MyTPA);
Writeln(inttostr(TPALength));
TPALength := GetArrayLength(MyTPA);
Writeln(inttostr(TPALength));
end.
- In this example I got the length of the TPA, MyTPA, and stored it in the variable TPALength, an integer. This is, as I stated earlier, useful when finding points and storing them all when you do not know how many different points you will get. You may then run through these points and check for something, like a specific uptext, for example. Some powerful, yet extremely simple, functions when manipulating TPAs are the following:
function High(x): Int64;
- By putting the TPA in for x, it will return the highest possible index for our TPA. The index is the number that goes in the brackets after the TPA name. MyTPA[2] has and index of 2. MyTPA[i] has an index of i. So if we have a TPA of length 10 (this means our TPA goes from 0 to 9, remember), High would return 9.
function Low(x): Int64;
- Similarly, Low will return the lowest possible index for our TPA. So if we have a TPA of length 10 that goes from 0 to 9, it will return 0. This is not actually particularly useful for TPAs, it is generally only used on static arrays. I just thought I would mention it. (Credits to mixster for helping me figure out what the use of Low was for :D).
program TPATutorial;
{.include SRL/SRL.scar}
var
MyTPA : TPointArray;
begin
SetLength(MyTPA, 10);
Writeln('High = '+inttostr(High(MyTPA)));
Writeln('Low = '+inttostr(Low(MyTPA)));
end.
- There is an example of how the functions High and Low work.