PDA

View Full Version : For DownTo / To Do [Statements]



Special Ed
08-16-2007, 07:10 PM
For DownTo / To Do [Statements]
By: Special Ed

Well this is my first tutorial so I hope this works out well and you can all understand what I'm trying to teach. This tutorial will teach you how to use for, to, downto & do in your scripts. These statements are used to repeat and action with a variable increasing or decreasing each time it loops. The basic format for these statements is as follows.

For To Do -
for i := 0 to 10 do Writeln(inttostr(i));

For DownTo Do -
for i := 10 downto 0 do Writeln(inttostr(i));

Note: These work like normal loops which means you can Break; out of them at any time, also you cannot use Goto statements out of them or into them.

For the first of these to statements it will Writeln i starting at 0 up to 10, as for the second statement it will do the opposite, it will writeln i starting at 10 down to 0, hence the downto. If you mix up to and downto in a statement accidentally it won't show any errors it just won't do anything for the statement. i.e.

for i:= 0 downto 10 do Writeln(inttostr(i));

This won't do anything because it starts at 0 and tries to go down but it can't so it just ends the loop. This is the same if you where to flip it around and try and write this...

for i:= 10 to 0 do Writeln(inttostr(i));

These statements are very helpful for all sorts of things, and are used widely in a number of different scripts. Almost everyone has seen these at least once if they've even seen FindFastRandoms, which is a very good example of for to do but it also uses case of statements, which I may make another tutorial for later.

So your probably wondering "Well what exactly am I supposed to use these for?" well I'll give you a few examples, they aren't to complicated just try and remember that all they are doing is "counting".

First Example is searching for a DTM "TreeDTM" starting at MSCX, MSCY and expanding the Box so it will find the TreeDTM closest to your character. This starts i at 1 because you can't find anything in a box that is 0 x 0.

for i := 1 to 10 do
if(FindDTM(TreeDTM, x, y, MSCX - (i * 51), MSCY - (i * 33), MSCX + (i * 51), MSCY + (i * 33)))then
Writeln('Found it!');

for i := 10 downto 1 do
if(FindDTM(TreeDTM, x, y, MSCX - (i * 51), MSCY - (i * 33), MSCX + (i * 51), MSCY + (i * 33)))then
Writeln('Found it!');

Note: I use (i * 51) and (i * 33) because those are the width and height of the main screen divided by 10.

If we used downto instead of to here we would be looking for the TreeDTM that is farthest away from us.

You can also using for to do statement to find Colors, or Bitmaps using Tolerance.
Note: With this I would probably do something different if it found the color so it would break out of the loop, because even if it found it when i was 3 it would still continue to look until i:= 10 unless you Break out of the loop.

for i := 0 to 10 do
if(FindColorTolerance(x, y, 123456, 0, 0, 500, 500, i))then
Writeln('Found It!');

for i := 10 downto 0 do
if(FindColorTolerance(x, y, 123456, 0, 0, 500, 500, i))then
Writeln('Found It!');

Same as the above, just this one starts looking for the color with a tolerance of 10 down to 1, right now they would both function as the same thing, since you don't break of the for to do loop in these examples. A good example of using these with Break; is this

for i := 10 downto 0 do
if(FindColorTolerance(x, y, 123456, 0, 0, 500, 500, i))then
begin
FoundIt:= True;
Break;
end;
if(FoundIt)then Mouse(x, y, 3, 3, true);

Note: I use the Boolean FoundIt here so that if it finds the color in the loop it will Break and Mouse. If I didn't have that boolean, even if it didn't find the color it would still Mouse to (x, y) .

Also another useful thing with for to do statements is that they can be stacked. Stacking a for to do statement looks like this.


for i := 0 to 10 do
for a := 0 to 2 do

Be warned though that stacking to many statements will create a lot of lag because its doing a lot of work ,but if you do use them just make sure you use Waits in there at least once.

What a stacked loop is literally doing is this,

i:= 0, a:= 0, a:= 1, a:= 2
i:= 1, a:= 0, a:= 1, a:= 2
ect...

So what your doing is multiplying the amount of "things" scar must do for the loop to end. Like for i:= 0 to 10 is eleven numbers and for a:= 0 to 2 is three numbers, so the amount of loops it does is 11 * 3 = 33 Loops! That is why it can create a lot of lag. Although they are useful you must use them wisely.

For a good example of stacked loops we can combine our first two for to do examples into one.

for i := 1 to 4 do
for a := 0 to 5 do
begin
Wait(5);
if(FindColorTolerance(x, y, 123456, MSCX - (i + 51), MSCY - (i * 33), MSCX + (i + 51), MSCY + (i * 33), a))then
Writeln('Found it!');
end;

So what this does is searches for the color 123456 starting in a box around MSCX, MSCY and a Tolerance of 0(a). So what it does is first searches in a box around MSCX and MSCY, and searches for the color 6 times in that first box with a tolerance from 0 to 5. It is that same as writing this, and this doesn't even search the whole screen, to search the entire main screen you'd have to double this and add a little more.

if(FindColorTolerance(x, y, 123456, MSCX - 51, MSCY - 33, MSCX + 51, MSCY + 33, 0))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 51, MSCY - 33, MSCX + 51, MSCY + 33, 1))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 51, MSCY - 33, MSCX + 51, MSCY + 33, 2))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 51, MSCY - 33, MSCX + 51, MSCY + 33, 3))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 51, MSCY - 33, MSCX + 51, MSCY + 33, 4))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 51, MSCY - 33, MSCX + 51, MSCY + 33, 5))then
Writeln('Found it!') else

if(FindColorTolerance(x, y, 123456, MSCX - 102, MSCY - 66, MSCX + 102, MSCY + 66, 0))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 105, MSCY - 66, MSCX + 102, MSCY + 66, 1))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 102, MSCY - 66, MSCX + 102, MSCY + 66, 2))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 102, MSCY - 66, MSCX + 102, MSCY + 66, 3))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 102, MSCY - 66, MSCX + 102, MSCY + 66, 4))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 102, MSCY - 66, MSCX + 102, MSCY + 66, 5))then
Writeln('Found it!') else

if(FindColorTolerance(x, y, 123456, MSCX - 153, MSCY - 99, MSCX + 153, MSCY + 99, 0))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 153, MSCY - 99, MSCX + 153, MSCY + 99, 1))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 153, MSCY - 99, MSCX + 153, MSCY + 99, 2))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 153, MSCY - 99, MSCX + 153, MSCY + 99, 3))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 153, MSCY - 99, MSCX + 153, MSCY + 99, 4))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 153, MSCY - 99, MSCX + 153, MSCY + 99, 5))then
Writeln('Found it!') else

if(FindColorTolerance(x, y, 123456, MSCX - 204, MSCY - 132, MSCX + 204, MSCY + 132, 0))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 204, MSCY - 132, MSCX + 204, MSCY + 132, 1))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 204, MSCY - 132, MSCX + 204, MSCY + 132, 2))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 204, MSCY - 132, MSCX + 204, MSCY + 132, 3))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 204, MSCY - 132, MSCX + 204, MSCY + 132, 4))then
Writeln('Found it!') else
if(FindColorTolerance(x, y, 123456, MSCX - 204, MSCY - 132, MSCX + 204, MSCY + 132, 5))then
Writeln('Found it!') else

I separated the i loops so you can see each time it expands the area. So you can see how useful for to do statements are.

I hope this tutorial has taught you a little about how for to do and for downto do statements work. If you have any questions please post and I'll answer them as best I can, and if I think of it add those answers to the tutorial.

The Element
08-16-2007, 08:56 PM
Cool! Thanks.

ShowerThoughts
08-16-2007, 08:57 PM
looks nice didnt read the whole thingy

Special Ed
08-17-2007, 03:58 AM
I hope this was a good enough of an explanation, if you have any questions about it or where confused at some parts please tell me and I'll try to explain, I think for to do statements are very essential to making good scripts, well at least I've found a lot of good uses for them :D.

ShowerThoughts
09-09-2007, 02:37 PM
bump!
i just that people see this amazing tut he worked hard on it and only 2 reply's:(

FrÕzÑ_§ÕµL
12-21-2007, 09:32 AM
Aweosme thanks I didn't know about downto, that might(or not) be useful! But hey, knowledge is power!

n3ss3s
12-21-2007, 09:56 AM
Woah, long tut :) Gj, also, frozn soul, thanks for grave digging it for me :)


Also, if you look at (ed this is not at you, the audience :p) radialroadwalk's code, you will notice that there is two for loops, one for that if StartRadial < EndRadial as "supposed" to, and one if its the otherway.

If StartRadial is for example 270 and EndRadial 180, it would start looking from 270 and go down to the 180, instead of going up from 180 to 270.

May sound minor, but if there is a big area of the color you might want to have a chance where to click :)

Special Ed
12-26-2007, 06:33 AM
Woah, long tut :) Gj, also, frozn soul, thanks for grave digging it for me :)


Also, if you look at (ed this is not at you, the audience :p) radialroadwalk's code, you will notice that there is two for loops, one for that if StartRadial < EndRadial as "supposed" to, and one if its the otherway.

If StartRadial is for example 270 and EndRadial 180, it would start looking from 270 and go down to the 180, instead of going up from 180 to 270.

May sound minor, but if there is a big area of the color you might want to have a chance where to click :)

Basically you're saying that a simple cure / idea on how to make to and downto more efficient when handling variables would be to make a statement saying something like...

if Variable < 50 then
for i:= 0 to 100 do Loop[i];

if Variable > 50 then
for i:= 100 downto 0 do Loop[i];

Ya thats a good idea although I don't know how often that would be used... Oh well someone has used it in the past and I'm sure someone will in the future.