SRL Archive - OSR Advanced Scripting Tutorials

Understanding and using TPoint/TPointArray


JAD

2007-06-26 21:45:56

[Tutorial]




Well, TPoint/TPointArray isn't super complicated, but for the most part only advanced/pretty advanced scripters use it, so I think it's a tutorial for the advanced :)

What you should know before reading this tutorial is:

Of course the grasp of all beginner concepts like variables…etc.

An understanding of how array's work.

for to do loops.




Anyways, let's begin.






TPoint


Well, TPoint is almost exactly like any other old variable, but can have 2 variables within the variable. Just take a look at this example:


procedure Whatever;
var Blah, J: TPoint;
begin
Blah.x := MSX1;
Blah.y := MSY1;
if(FindColor(J.x, J.y,1231251, Blah.x, Blah.y, MSX2, MSY2))then
Mouse(J.x,J.y,1,1,true);
end;

See? So pretty much, TPoint is just a variable that you can have 2 variables inside of it, x and y.

So you may be asking, why the heck would I use this? I could just use normal variables couldn't I? Well.. Take a look at this function in SRL:


{*******************************************************************************
function ItemCoords(i: Integer): TPoint;
By: RsN
Description: Returns X and Y of Specified Inventory Spot
*******************************************************************************}

function ItemCoords(i: Integer): TPoint;
var
row, col: Integer;
begin
row := i div 4;
col := i - (row * 4) - 1;
if (col < 0) then
begin
col := 3;
row := row - 1;
end;
Result.x := 536 + ((col + 1) * 42) + Random(7);
Result.y := 184 + ((row + 1) * 36) + Random(7);
end;

Now, you may be asking, "Well why couldn't they just use a regular variable there too??". Well, think about it. If they made it

function ItemCoords(I: Integer): Integer;

That wouldn't work for this function. It wouldn't work because you can only save 1 number to the result of an integer function, but there are 2 variables that need to be saved, x and y. So you have to save both of them to the result of the function, and the only way to do that is by using TPoint.

So this is how you could use those variables from the TPoint in that function then:


procedure Whatever;
var J: TPoint;
Ax,Ay: Integer;
begin
J := ItemCoords(1);
Ax := J.x
Ay := J.y
Mouse(Ax,Ay,1,1,true);
end;

See how we did that? when we do J := ItemCoords(1), it adds the .x and .y to the TPoint J because those are the results. And then you can make Ax and Ay = J.x and J.y like so.

Hope I explained that well! Moving on to TPointArray.






TPointArray

Well, TPointArray, hence the name, is just an array and TPoint combined :)

So I'm going to show you an example of it, and then explain it below if you didn't get it just from looking through the code:


procedure Whatever;
var
OurVar: TPointArray;
i: integer;
begin
for i := 1 to 28 do
begin
OurVar[i]:= ItemCoords(i); //Makes all of OurVar's = there ItemCoords number
Mouse(OurVar[i].x, OurVar[i].y,1,1,true);
end;
end;

OK, see that now? Pretty much, an array (with numbers) is Whatever[1] and Whatever[2]….etc. and a TPoint is Whatever.x, Whatever.y. So we are making 2 variables in a number of the array.

So we make OurVar 1-28 = there ItemCoords by number. So it saves it as OurVar[1].x and OurVar[1].y and OurVar[2].x and OurVar[2].y…….etc.

Then we go through and we click the mouse at each inventory slot :)





Well, that's the end of the tutorial. I hope all that clicked and I explained it well and you are on your way to using TPoint/TPointArray in your scripts :)

~JAD




[/Tutorial]

gamer716

2007-06-26 21:57:10

nice tutorial but i dont think i will using these lol

JAD

2007-06-26 21:58:24

From: gamer716
nice tutorial but i dont think i will using these lol


You may as you get more advanced.. ;) And thanks :)

WhiteShadow

2007-06-26 23:28:26

It doesn't seem like you know this topic very sorry, I speak the truth.

procedure Whatever;
var Blah, J: TPoint;
begin
Blah.x := MSX1;
Blah.y := MSY1;
Blah.x1 := MSX2;
Blah.y1 := MSY2;
Blah.Poopy := 1259068;
if(FindColor(J.x, J.Laa, Blah.Poopy, Blah.x, Blah.y, Blah.x1, Blah.y1))then
Mouse(J.x,J.Laa,1,1,true);
end;

This is incorrect. TPoint can only end in .x, .y unless
it's using a custom type variable. The TPoint is suppose to be like a coord, an (x, y) so to speak.

procedure Whatever;
var OurVar: Array[1..28] of TPointArray;
I: Integer;
begin
for I := 1 to 28 do
OurVar[i] := ItemCoords[i]; //Makes all of OurVar's = there ItemCoords number
for I := 1 to 28 do
Mouse(ItemCoords[i].x,ItemCoords[i].y,1,1,true);
end;

array[1..28] of TPointArray 
^That would be an array of TPointArray;

So instead of a regular TPointArray:

FindColorsTolerance(Pts, color, msx1, msy1, msx2, msy2, 5);

You have

FindColorsTolerance(Pts[length], color, msx1, msy1, msx2, msy2, 5);

In your case 28 different TPointArrays;

Is suppose to be:

OurVar : TPointArray; 

Fixed:

procedure Whatever;
var
OurVar: TPointArray;
i: integer;
begin
SetArrayLength(OurVar, 28);
for i := 1 to 28 do
OurVar[i] := ItemCoords[i]; //Makes all of OurVar's = there ItemCoords number
for i := 1 to 28 do
Mouse(ItemCoords[i].x,ItemCoords[i].y,1,1,true);
end;

Which is pretty useless making more variables when you can just use:

procedure Whatever;
var
OurVar: TPoint;
i: integer;
begin
for i := 1 to 28 do
begin
OurVar:= ItemCoords[i]; //Makes all of OurVar's = there ItemCoords number
MMouse(OutVar.x, OutVar.y);
end;
end;

JAD

2007-06-27 12:01:08

Wow, ok I didn't know that.. I've only used TPoint/TPointArray for .x and .y, but YoHoJo said it could be .WhateverYouWant so I assumed it could :)

Thanks though WhiteShadow.

shaunthasheep

2007-06-27 12:49:49

program GoodStuff;
{.include SRL/SRL.scar}

type
IsOnCrack = record
x, y, x1, y1, poopy, LAA : integer;
end;

procedure Whatever;
var
JAD:IsOnCrack;
begin
JAD.x := MSX1;
JAD.y := MSY1;
JAD.x1 := MSX2;
JAD.y1 := MSY2;
JAD.Poopy := 1259068;
if(FindColor(JAD.x, JAD.Laa, JAD.Poopy, JAD.x, JAD.y, JAD.x1, JAD.y1))then
Mouse(JAD.x,JAD.Laa,1,1,true);
end;

begin
Whatever;
end.

Lol JAD

JAD

2007-06-27 14:31:56

From: The_Rs_Monkey
program GoodStuff;
{.include SRL/SRL.scar}

type
IsOnCrack = record
x, y, x1, y1, poopy, LAA : integer;
end;

procedure Whatever;
var
JAD:IsOnCrack;
begin
JAD.x := MSX1;
JAD.y := MSY1;
JAD.x1 := MSX2;
JAD.y1 := MSY2;
JAD.Poopy := 1259068;
if(FindColor(JAD.x, JAD.Laa, JAD.Poopy, JAD.x, JAD.y, JAD.x1, JAD.y1))then
Mouse(JAD.x,JAD.Laa,1,1,true);
end;

begin
Whatever;
end.

Lol JAD


That wasn't very nice.. :( (half kidding).

And that's how I was taught (not blaming YoHoJo), so that's how I thought it was :)

I fixed the tutorial already though :p

Wizzup?

2007-06-27 14:47:33

From: JAD
a look at this example:


procedure Whatever;
var OurVar: Array[1..28] of TPointArray;
I: Integer;
begin
for I := 1 to 28 do
OurVar[I] := ItemCoords[I]; //Makes all of OurVar's = there ItemCoords number
for I := 1 to 28 do
Mouse(ItemCoords[I].x,ItemCoords[I].y,1,1,true);
end;


Isn't ItemsCoord a function in which you put a number?
It should be ItemCoords(i) and Not itemCoords
And again, for the second loop, that won't work.
Use For I := 1 to 28 do
MMouse(OurVar.X, OurVar.Y, 1, 1)

Thats about it.. :)

EDIT: I Guess I'll make one later.. ;)

JAD

2007-06-27 15:05:18

Thanks for making me feel sooo great about my work guys! it really makes me feel great that everybody is behind how well I wrote this tutorial. :redface:

I didn't test anything in this.. And I just wrote it from my head, so maybe there were some typos :redface:

NewToAutoing

2007-06-27 15:15:28

Lol, i was just about to post and say nearly everything everyone else just said, but then saw they beat me to it. Just fix it up, and it'll be a great tut, like the rest of yours ;) And i'm sure people will find it useful, i don't even remember how i learned what they were myself, because i don't think i could find a tutorial on them :p

JAD

2007-06-27 15:24:56

From: NewToAutoing
Lol, i was just about to post and say nearly everything everyone else just said, but then saw they beat me to it. Just fix it up, and it'll be a great tut, like the rest of yours ;) And i'm sure people will find it useful, i don't even remember how i learned what they were myself, because i don't think i could find a tutorial on them :p


How were you about to say nearly everything everybody else said?? I fixed the tutorial like 4 hours ago…

Maybe you were just saying that to seem smart? ;)

NewToAutoing

2007-06-27 17:53:25

The TPointArray still has errors that people stated..unless i'm seeing things..

Edit: Though i should have been more specific to state that i was going to say what they said about the TPointArray part..

Edit2: To be specific, the array of TPointArray is still there, where you could have just done array of TPoint (if you for some reason felt the necessity of setting the length in that one line) or just put TPointArray with SetArrayLength as the first line of it. Then the part about ItemCoords should be ItemCoords(I).

Edit3: Nevermind me pointing everything out, just change it to this
From: WhiteShadow

procedure Whatever;
var
OurVar: TPoint;
i: integer;
begin
for i := 1 to 28 do
begin
OurVar:= ItemCoords[i]; //Makes all of OurVar's = there ItemCoords number
Mouse(OurVar.x, OurVar.y,1,1,true);
end;
end;


That's the best way it should be, actually. Fixed a typo in it, that's all. Changed MMouse to Mouse since that's what you had in your original example. Typo was OutVar instead of OurVar in the mouse part.

shadows-collide

2007-06-27 18:41:50

Cut the guy some slack, I didnt understand T vars at all before this lol, now I sort of understand, so I think its good, and now its fixed its even better :)

Good job!

JAD

2007-06-27 18:54:43

Thanks NewToAutoing, and sorry if I came off a little rude :p

And I fixed that now :)

Edit: But that example would make OurVar = ItemCoord(I) and change it each time, not save it 28 times to OurVar[1] := ItemCoords[1]…etc. right?

NewToAutoing

2007-06-27 19:33:09

Right, which is fine cause we don't need all the coords saved anyway once they've been used :)

Sorry if i came off a little rude too, just that you were (or started off) kind of hard on my app, and then again slightly rude to me here :p No hard feelings though.

All you gotta do now is fix the explanation since it still refers to the TPo-…I just realized it doesn't use a TPointArray anymore..So it doesn't work as an example :eek: I was gonna say fix the explanation since it refers to OurVar being a TPointArray when it's just a TPoint now, and noticed that. Well..whatever :p Still a great tut.

LordGregGreg

2007-06-27 19:58:02

Don't worry about it jad! Sometime people leasrn more form other people mistakes than they do being taught it right. I apreciated your tutorial and it was very helpfull to me. (I need to like, completly rewrit esome of my functiosn with vars like, newx, newy, oldx, oldy) lol.

WhiteShadow

2007-06-28 20:31:09

JAD! Good job!

You have a huge red bold/TPointArray title but you don't have any TPointArray examples. As Wizzup? pointed out before it's ItemCoords(i).

:) :p :) :p :) :p

JAD

2007-06-28 23:50:21

From: WhiteShadow
JAD! Good job!

You have a huge red bold/TPointArray title but you don't have any TPointArray examples. As Wizzup? pointed out before it's ItemCoords(i).

:) :p :) :p :) :p


Gah I missed that! :p

And I made it so that we are using TPointArray now… :p Even though it really isn't needed to use TPointArray there, you could just use TPoint, it would be needed if you were going to use those later, and it's just an example :p

I may make an example using FindColorsSpiralTolerance later.. :p

Santa_Clause

2007-06-30 08:55:58

Gah. This is more confusing than leet talk.

Wanted

2007-07-01 22:17:39

From: SantaClause
Gah. This is more confusing than leet talk.


Skip learn'n much?

duncann3

2008-09-25 19:57:49

cheers