PDA

View Full Version : for i := x to y do - How to use them.



Sin
03-16-2012, 02:54 AM
for..to..do loops are very efficient to use.
They're especially useful for tasks such as cleaning herbs, where you have to click each and every inventory slot.
for i := 1 to 28 do

is what you'll see in most cases (if regarding the inventory).
This'll be very short, but simple guide on how to use them.
Take my scatterer script's Scatter procedure -
procedure Scatter;
var
i:Integer;
begin
MouseSpeed := 255;
MouseItem(1,1);
MouseItem(5,1);
MouseItem(9,1);
MouseItem(13,1);
MouseItem(17,1);
MouseItem(21,1);
MouseItem(25,1);
MouseSpeed := 18;
MouseItem(2,1);
MouseSpeed := 255;
MouseItem(6,1);
MouseItem(10,1);
MouseItem(14,1);
MouseItem(18,1);
MouseItem(22,1);
MouseItem(26,1);
MouseSpeed := 18;
MouseItem(3,1);
MouseSpeed := 255;
MouseItem(7,1);
MouseItem(11,1);
MouseItem(15,1);
MouseItem(19,1);
MouseItem(23,1);
MouseItem(27,1);
MouseSpeed := 18;
MouseItem(4,1);
MouseSpeed := 255;
MouseItem(8,1);
MouseItem(12,1);
MouseItem(16,1);
MouseItem(20,1);
MouseItem(24,1);
MouseItem(28,1);
end;
What happens in that procedure is, it'll click each an every inventory slot at VERY high speeds, and its natural for some of the clicks to not go through.
What we can add at the end of the procedure is -
repeat
for i := 1 to 28 do
begin
if ExistsItem(i) then
MouseItem(i,1);
end;
Until(InvEmpty);
What that'll do is it'll cycle through i, which is an array of sorts of 1 to 28.
If an item exists in *i*, then it'll click it.
Test it out, play around with it, it'll come in real useful one day.


Sorry if you can't understand it, it's 11PM here and I was awake the whole day starting from 5AM ;s

weequ
03-16-2012, 03:31 AM
Nice tutorial.

One would be nice that explains while and until in the same tutorial too :p

p.s Integer i isn't really an array :D

Sin
03-16-2012, 03:33 AM
I'll do that :p

weequ
03-16-2012, 03:37 AM
I should make some tutorials too, could be fun.

Mat
03-17-2012, 12:29 PM
Didn't know that, gonna find it useful
Thanks x

masterBB
03-17-2012, 01:53 PM
for i := 1 to 28 do
begin
if ExistsItem(i) then
MouseItem(i,1);
end;

equals


i := 1;
repeat
if ExistsItem(i) then
MouseItem(i,1);
Inc(i);
until(i > 28);

So i really isn't an array, just a variable of the type integer.

smurg
03-18-2012, 04:25 AM
Fix your standards, use some failsafes, and why would your inventory ever become empty cleaning herbs?

Sin
03-18-2012, 04:51 AM
Fix your standards, use some failsafes, and why would your inventory ever become empty cleaning herbs?

Did you ever read what script is about?
Jeez, atleast look before posting and making a fool out of yourself..

smurg
03-18-2012, 05:14 AM
Did you ever read what script is about?
Jeez, atleast look before posting and making a fool out of yourself..

This is a tutorial. People copy blocks of code from tutorials or attempt to learn from them. You should teach solid fundamentals of all aspects of scripting in the code blocks whether it's the intended subject or not.

You mentioned cleaning herbs and then a scatterer function in your Scatter script. I'm guessing it's supposed to scatter ashes now, but from the flow of the paragraphs, I missed that originally.

This isn't even a tutorial on for loops, it's you taking a for loop code block from a script and telling them how it clicks inventory slots. You should go over declaring variables, initializing, using appropriate failsafes, and when to use it over other loops or other loops over it. (Maybe a more descriptive thread title also)

Don't take it personally. Just if I see sloppy work posted, I put in my 2 cents.

Sin
03-18-2012, 05:19 AM
Nowhere in the guide do I say that this is a tutorial :s

simple guide on how to use them.
Thanks for your opinion though, it's noted.

smurg
03-18-2012, 05:27 AM
SRL-Forums (http://villavu.com/forum/index.php) > Tutorial Island (http://villavu.com/forum/forumdisplay.php?f=28) > Scripting Tutorials (http://villavu.com/forum/forumdisplay.php?f=191) > Tutorial for Beginners (http://villavu.com/forum/forumdisplay.php?f=95)

Sin
03-18-2012, 05:41 AM
Where else am I supposed to put it then?

Joe
04-13-2012, 05:33 PM
Very nice tutorial. Thanks a lot!

Home
05-31-2012, 01:08 PM
Not sure, but but do you mean that I replace "Scatter" function with one below?

Just asking for new ones :)

~Home

mike-gee420
06-04-2012, 05:49 PM
Thanks shay! :) I didn't really understand this until now,
seems easier than i first thought. :duh: