SRL Archive - OSR Advanced Scripting Tutorials

What are TPointArrays(TPA's) and Some Functions & Procedures to Use 'Em With!


Bionicle

2009-09-20 23:37:29

Hello, welcome to my 5th tut fellow and future scriptures!

Breaking it Down

TPoint

Remember way back in grade 6 math when you had that graph with the x axis and the y axis and the teacher gave you coordinates for points on the graph then asked you to put them in, connect the dots and make a picture of a cat?
Each of those (x, y) points were known as 'points'. We call them 'TPoints'. Not to hard ;). For a more technical and shorter definition: "A specified point on the cartesian plane".

Arrays

Arrays are a simple way of declaring a bunch of variables that have some similarities. They can be any variables, but the most commonly used are booleans, strings, extendeds and integers. As for an example, here is a simple script i stole from NaumanAkhlaQ's array tut:
From: NaumanAkhlaQ
Procedure CutTree;
Var
YewColor : Array [0..5] of Integer;
i :Integer;
Begin
YewColor[0]:=2182984;
YewColor[1]:=1855560;
YewColor[2]:=4426622;
YewColor[3]:=1914933;
YewColor[4]:=3239519;
YewColor[5]:=3116158;
Begin
If (not (Loggedin)) then
Exit;
else
For i:= 0 to 5 do
If FindObjCustom(x,y,['ew'],[YewColor[i]],5) then
Begin
If IsUpText('ew') then
Begin
CutDown;
end;
end;
end;


Understanding the Use of TPA's

Now that you understand what a TPoint is, you can also understand that if you use TPoints in a script, and you need multiple TPoints for, lets say, finding an area on the minimap, it would take up a lot of space. It would take up a lot of space because this is how you would give a TPoint a value if you had multiple TPoints, without the use of TPAs:
program TPointExample;
var
Point1, Point2, Point3, Point4, Point5, Point6, Point7, Point8, Point9: TPoint;

begin
Point1:= inttopoint(523, 624); //inttopoint is short for integer to point. this takes two integers, in this case an x and a y, and converts it into one TPoint.
Point2:= inttopoint(253, 734);
Point3:= inttopoint(954, 736);
Point4:= inttopoint(1163, 712);
Point5:= inttopoint(52, 977);
Point6:= inttopoint(10, 13);
Point7:= inttopoint(535, 324);
Point8:= inttopoint(16, 61);
Point9:= inttopoint(233, 42);
end.

Thats alot of text :eek:.

Setting and Finding the Length of a TPA

First of all, we need to declare the TPA:

var
TheTPA: TPointArray;
Simple as that ;)

Now here are five procedures/functions that can be used for setting and finding the length of a TPA, I will go into more depth later.

procedure SetLength(var s; NewLength: Integer);
procedure SetArrayLength(var Arr; Count: Integer);
function Length(s): Integer;
function GetArrayLength(var Arr): Integer;
function High(x): Int64;

SetLength: This takes the TPointArray specified (var s), and changes the length to the length specified (NewLength). Heres an example:
SetLength(TheTPA, 5);
This will take the TPA 'TheTPA' and change its length to 5.
SetArrayLength: Very similar to Setlength. It takes the TPointArray specified (var Arr), and changes the length to the length specified (Count). Example:
SetArrayLength(TheTPA, 5);
As you can see, it is almost the exact same, except for the procedure title.
Length: This takes the length of the specified TPA (s) and returns it as an integer. Example:
var
TheTPALength: integer;
TheTPA: TPointArray;

begin
TheTPALength := Length(TheTPA);
WriteLn('The length of TheTPA is ' + inttostr(TheTPA) + '.');
end.
This finds the length of the TPA 'TheTPA' and stores it as the integer 'TheTPALength', then it transfers the integer into a string and writes the number in the debug box.
GetArrayLength: This takes the length of the specified TPA (var Arr) and returns it as an integer. Example:
var
TheTPALength: integer;
TheTPA: TPointArray;

begin
TheTPALength := GetArrayLength(TheTPA);
WriteLn('The length of TheTPA is ' + inttostr(TheTPA) + '.');
end.
This is almost the same thing as Length, except just a different name.
High: This will find the highest index from out TPA. (e.g., TheTPA[0..10] has a highest index of 10.) Example:
Writeln('The highest index for TheTPA is ' + inttostr(High(TheTPA)));

MiddleTPA

This may sound confusing, but it is really easy once you get it. MiddleTPA finds the exact center of all of your TPA's. NOT the middle number of all your indexes, but the center of all your coordinates. Maybe this will help:

The x's are your TPoints. The check mark is the exact center of the four TPoints (yes it is, i measured it ;)).

Putting it all Togeather

program TPATogeatherExample;

var
TheTPALength: Integer;
TheTPoint: TPoint;
TheTPA: TPointArray;

begin
SetArrayLength(TheTPA, 6); //TheTPA now has 6 indexes
TheTPALength := Length(TheTPA); //Giving TheTPA length as the length of TheTPA
WriteLn('The length of TheTPA is ' + inttostr(TheTPA) + '.');
TheTPA[0]:= inttopoint(613, 834); //Declaring the coordinates...
TheTPA[1]:= inttopoint(336, 236);
TheTPA[2]:= inttopoint(723, 443);
TheTPA[3]:= inttopoint(232, 723);
TheTPA[4]:= inttopoint(234, 442);
TheTPA[5]:= inttopoint(633, 666);
TheTPoint := MiddleTPA(TheTPA); //This declares TheTPoint as the point in the exact center of the other 6 TPoints.
Writeln('The coordinates of TheTPoint are: (' + inttostr(TheTPoint.x) + ', ' + inttostr(TheTPoint.y) + ').'); //TheTPoint.x is the x coordinate of TheTPoint, same with TheTPoint.y, only with y.
end.

Thankyou for reading my first advanced tut. Any questions, comments or corrections, please post below. If you liked my tut, click the blue check mark in the top right corner ;).

Dynamite

2009-09-20 23:41:59

Doing well, keep it up.
Rep+

T~M

Awkwardsaw

2009-09-20 23:49:46

where did you get that pick of the barrow tunnels? isnt the minimap black?

and nice tut too :p

and for your arrays,


TheTPA := [inttopoint(613, 834), inttopoint(336, 236), inttopoint(723, 443)
inttopoint(232, 723), inttopoint(234, 442), inttopoint(633, 666)]

Quickmarch

2009-09-20 23:50:13

Shorter way of writing a static TPointArray:


YourTPA := [Point(454, 676), Point(0, 0), Point(5, 5), ...];

Edit: Ninja'd, but you edited your post. :(

All that is man

2009-09-20 23:51:03


WriteLn('The length of TheTPA is ' + inttostr(TheTPA) + '.');
Should be

WriteLn('The length of TheTPA is ' + inttostr(TheTPALength) + '.');

Just saying, otherwise very nice!

Bionicle

2009-09-21 01:39:28

From: Awkwardsaw
where did you get that pick of the barrow tunnels? isnt the minimap black?

and nice tut too :p

and for your arrays,


TheTPA := [inttopoint(613, 834), inttopoint(336, 236), inttopoint(723, 443)
inttopoint(232, 723), inttopoint(234, 442), inttopoint(633, 666)]


found the barrows thingy on google images
i added in the x's and check mark and uploaded on imageshack:p

Smarter Child

2009-09-22 03:50:04

N1ce. Keep up the awesome work, you're really active in the community lately :D.

I <3 TPAs btw. :p

Anyways, add more functions from the wizzy plugin. Such as, SplitTPA, SplitTPAEx which splits the TPointArray, CombineTPA: combines the TPA [sometimes done after splitting it, kinda useful for trees :)] Also add MiddleTPAEX, its a boolean so you can use it as a failsafe and doesn't need a TPoint declared. Those are pretty much the most commonly used functions, unless you want to go more in depth and explain aTPA's.

E = Also include that Length and GetArrayLength do the same thing, but i think GetArrayLength is faster..[might b wrong]. Also assigning an integer to High can be faster since each time you use For i := 0 to High(TPA) do, it becomes slower. You could also do Length(TPA)-1, which does the same thing as High. OMG, why am i explaining all this to you :p, you probably know this stuff already. :D [Lol, don't take anything offensive, because some people do when you explain things to them, that i already know :p].

Good job and good luck~

:)

Rick

2009-09-26 17:02:01

Nice Tutoria :)l,


procedure SetLength(var s; NewLength: Integer);
procedure SetArrayLength(var Arr; Count: Integer);
function Length(s): Integer;
function GetArrayLength(var Arr): Integer;
function High(x): Int64;

In addition to
function High(x): Int64;
there is also:

function Low(x): Int64;

Bionicle

2009-09-26 17:08:51

From: r!ch!e
Nice Tutoria :)l,


procedure SetLength(var s; NewLength: Integer);
procedure SetArrayLength(var Arr; Count: Integer);
function Length(s): Integer;
function GetArrayLength(var Arr): Integer;
function High(x): Int64;

In addition to
function High(x): Int64;
there is also:

function Low(x): Int64;


yea, but i didn't think it was important, as low will always be the same.:)

marpis

2009-10-04 23:14:49

I think this belongs to Intermediate section.