SRL Archive - OSR Help

How do I set a procedure to repeat X times?


Runehack123

2012-10-21 07:24:43

Hi Simba community :) ,

I watched YoJoJo's TuT and his gold fish script worked well, but it got out of control really fast and I couldn't stop with CTRL+S or using F2 or FN+ALT+F4. I had to log out of my PC to crash the plug-in! So many goldfish were saved it was scary…

So here's the thing. I want to save a set amount of goldfish and then make the script stop once it has saved that amount. I've been trying for days now reading all about loops and repeats - I even messaged YoHoJo, but it's the weekend and was hoping if I just put out there I might get a reply from someone! :)

I tried the "For" loop, but all it seems to do is type 0 0 0 0 0 0 in the debug box. I know there's something I'm doing wrong - please help me!!

Current script:

program new;

var
XFish: Integer;

//http://www.i-am-bored.com/bored_link.cfm?link_id=10574

procedure StartGame;
begin
MoveMouse(350, 160)
ClickMouse(350, 160, 1);
Wait(1000);
end;

procedure SaveGoldfish;
var
x, y:Integer;

begin

if FindColorSpiralTolerance(x, y, 1471228, 16, 33, 266, 235, 20) or
FindColorSpiralTolerance(x, y, 1204732, 16, 33, 266, 235, 20) or
FindColorSpiralTolerance(x, y, 549886, 16, 33, 266, 235, 20) or
FindColorSpiralTolerance(x, y, 869629, 16, 33, 266, 235, 20) Then
begin
MoveMouse(x, y);
HoldMouse(x, y, 1);
MoveMouse(306, 103);
Wait(2000);
ReleaseMouse (306, 103, 1);
Inc(XFish);
Writeln('We have saved a Goldfish ' + IntToStr(XFish) + ' times');
end;

end;


begin
StartGame;
repeat
SaveGoldfish;
until(XFish >= 4);
end.
>>>>>>>>>>>>>>>>>>
debug box:

Compiled successfully in 15 ms.
We have saved a Goldfish 1 times
We have saved a Goldfish 2 times
We have saved a Goldfish 3 times
We have saved a Goldfish 4 times
Successfully executed.

>>>>>>>>>>>>>>>>>>

I thought that creating an integer 'XFish' it would go through that loop everytime and increase by 1 until it got to 4.
Why isn't that? What am I doing wrong? It seems to recognise the timer of 2s I set, but I want it to increase by 1 every time I run the whole procedure and not count from 1 to 4 in 2s intervals and then stop. :(

Please help me :) - right now it feels like I hit a solid brick wall. Loops and repeats looked simple in the guides so it's so frustrating to see that I can't even make it save X amounts of fish :( … I don't want it to go on forever!

Got my fingers crossed for a reply :) ~

riwu

2012-10-21 07:30:46

Is StartGame required to repeat another round? If so you should include it in your loop. Also since you are doing it only 4 times it's going to end extremely fast.

To use a for loop:
for i:=1 to 4 do
SaveGoldfish;
Declare i as Integer.

Daniel

2012-10-21 09:31:54

CTRL + ALT + S stops the script from running any further :)

Also, it is advised to just post the question instead of PM'ing somebody if you have a scripting problem, as you are most likely to get a much faster response that way. ;)

Runehack123

2012-10-21 11:55:45

From: riwu
Is StartGame required to repeat another round? If so you should include it in your loop. Also since you are doing it only 4 times it's going to end extremely fast.

To use a for loop:
for i:=1 to 4 do
SaveGoldfish;
Declare i as Integer.


Start game just presses the start button on the flash-game. Once the game has started it no longer needs to run since you just want to save the gold fish over and over again, dragging them from a frying pan into a fish bowl…

I applied your advice to my script and here's what happened:
Compiled successfully in 47 ms.
We have saved a Goldfish 5 times
Successfully executed.

It was over almost instantly when the game started and no fish was even clicked on or scrolled over. The mouse just remained where it was - procedure 'SaveGoldfish' was never executed!

>>>>>>>>>>>>>>>>>>>>>>>>>>>>
program new;

var
i: Integer;

//http://www.i-am-bored.com/bored_link.cfm?link_id=10574

procedure StartGame;
begin
MoveMouse(350, 160)
ClickMouse(350, 160, 1);
Wait(1000);
end;

procedure SaveGoldfish;
var
x, y:Integer;

begin

if FindColorSpiralTolerance(x, y, 1471228, 16, 33, 266, 235, 20) or
FindColorSpiralTolerance(x, y, 1204732, 16, 33, 266, 235, 20) or
FindColorSpiralTolerance(x, y, 549886, 16, 33, 266, 235, 20) or
FindColorSpiralTolerance(x, y, 869629, 16, 33, 266, 235, 20) Then
begin
MoveMouse(x, y);
HoldMouse(x, y, 1);
MoveMouse(306, 103);
Wait(2000);
ReleaseMouse (306, 103, 1);
end;

end;


begin
StartGame;
for i:=1 to 4 do
SaveGoldfish;
Writeln('We have saved a Goldfish ' + IntToStr(i) + ' times');
end.

Footy

2012-10-21 12:14:18

The "for I := 1 to 4" line means it will execute "SaveGoldFish" 5 times. If you want to do it more times, then increase the 4 to 400, or more.

edit: The reason your mouse didn't move was because the script had just started, and the fish hadn't popped up yet, thus no orange fishie colors were found.

Runehack123

2012-10-21 12:26:38

From: Footy
The "for I := 1 to 4" line means it will execute "SaveGoldFish" 5 times. If you want to do it more times, then increase the 4 to 400, or more.

edit: The reason your mouse didn't move was because the script had just started, and the fish hadn't popped up yet, thus no orange fishie colors were found.


Ok, so if this is the case then this should do the trick:
begin
StartGame;
Wait(10000);
for i:=1 to 4 do
SaveGoldfish;
Writeln('We have saved a Goldfish ' + IntToStr(i) + ' times');
end.

However, only 1 fish is saved and then the script stops.

debug:
Compiled successfully in 32 ms.
We have saved a Goldfish 5 times
Successfully executed.

Also, for i:=1 to 4 do is only for testing I want to see if I'm getting the exact amounts right, which im not. I can always increase that afterwards, but right now its just not working even though its meant to be working. Just cant see what im doing wrong here :(

EDIT:
just tried increasing 'to x'
begin
StartGame;
Wait(10000);
for i:=1 to 100 do <<<<<<
SaveGoldfish;
Writeln('We have saved a Goldfish ' + IntToStr(i) + ' times');
end.

debug reads:
Compiled successfully in 31 ms.
We have saved a Goldfish 101 times
Successfully executed.

After I saved a goldfish ONCE.

Any idea whats happening here?

Footy

2012-10-21 12:28:29

SaveGoldfish take ~10ms to execute. Unless fish are popping up at 1 fish per 10ms, executing Savegoldfish 5 times will not actually save 5 fish.

Runehack123

2012-10-21 12:57:22

I see..
Well, at the start theres ~1 goldfish every 3 seconds or so, but the game picks up in pace as time progresses. Although, even at a late-game stage 10ms/goldfish wouldnt apply since dragging it and dropping it will have more delay than that. The minimum wait time I can set when it hovers over the bowl is 50ms (otherwise it doesn't run smooth with internet lag etc…).

However, really do want to run the procedure so that when a goldfish is actually rescued i increases and not just so its 10ms/+1 like you said.

How would I do that?
Back to the topic of this:
HOW DO I RUN A PROCEDURE X AMOUNT OF TIMES?

What For i:=1 to 100 seems to be doing is running through the thing 100 times rapidly regardless of whether the procedure is carried out or not.
Seems a bit useless to me?
Please help!

litoris

2012-10-21 13:08:36

You need to make the procedure a boolean function, and use waitfunc to wait until the fish is caught. You need a method to determine when a fish is caught though.

Runehack123

2012-10-21 13:13:50

wow that sounds really complicated.
I'd have no idea how to do that off the top of my head :) .
Mind giving me a quick example?
Or I'll probably end up poking around in one of the guides and see how that goes!
Thanks, what you said sounds promising even though I have no idea how to do that..

PS: Fish is caught for a fact everytime it does this—->

begin
MoveMouse(x, y);
HoldMouse(x, y, 1);
MoveMouse(306, 103);
Wait(2000);
ReleaseMouse (306, 103, 1);
end;

Which is moving the mouse over to where the fish is found and putting it in the bowl. It's all part of the SaveGoldfish procedure though.

More importantly though, how will setting my procedure to a boolean function help save X amount of fish? I just don't understand how this will work.

riwu

2012-10-21 20:47:30

He only increase the integer when the color was found, so if the debug says the fish is caught it means it actually found the color and successfully executed SaveGoldFish.

Try add ActivateClient to the start of the main program (a line before the StartGame procedure in your main program). Maybe you didn't alt tab fast enough, but there is a whole 2 sec wait there so it's unlikely too.

A boolean function works like this:
function SaveGoldFish: Boolean;
begin
if FindColorblabla then
begin
//some actions
Result:= True; //assigns result to true only if color is found
end;
end;

Then to use it,
repeat
if SaveGoldFish then //only perform Inc(x) if you successfully found the color
Inc(x);
until false;

but you have already done something similar so that's not the problem.

Runehack123

2012-10-22 00:07:52

Thank you,
here's what happened:

We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 26 times
We have saved a Goldfish 27 times
We have saved a Goldfish 28 times
We have saved a Goldfish 29 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 30 times
We have saved a Goldfish 31 times
We have saved a Goldfish 32 times
We have saved a Goldfish 33 times
We have saved a Goldfish 34 times
We have saved a Goldfish 35 times
We have saved a Goldfish 36 times
We have saved a Goldfish 37 times
We have saved a Goldfish 38 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
We have saved a Goldfish 39 times
>>>>>>>>>>>>>
i seemed to increase once when the fish was grabbed and then after it was dropped in the bowl again. So 2x / fish.
Also, the script didnt stop like I hoped it would…

>>>>>>>>>>>>>
function SaveGoldfish: Boolean;
var
x, y:Integer;

begin

if FindColorSpiralTolerance(x, y, 1471228, 16, 33, 266, 235, 20) or
FindColorSpiralTolerance(x, y, 1204732, 16, 33, 266, 235, 20) or
FindColorSpiralTolerance(x, y, 549886, 16, 33, 266, 235, 20) or
FindColorSpiralTolerance(x, y, 869629, 16, 33, 266, 235, 20) Then
begin
MoveMouse(x, y);
HoldMouse(x, y, 1);
MoveMouse(306, 103);
Wait(2000);
ReleaseMouse (306, 103, 1);
Result:= True;
end;

end;

begin
StartGame;
For i:=1 to 10 do
repeat
if SaveGoldfish then
Inc(i);
Writeln('We have saved a Goldfish ' + IntToStr(i) + ' times');
until false;
end.
>>>>>>>>>>>>

Why doesn't it stop at 10? And why does it increase twice for every goldfish saved?
Thanks for all the replies so far :)

riwu

2012-10-22 00:31:27

If you use a for loop, you dont have to do Inc(i) anymore as it automatically increases after every loop. And for is already a loop so you don't need another loop.
begin
StartGame;
repeat
if SaveGoldfish then
begin
Inc(i);
Writeln('We have saved a Goldfish ' + IntToStr(i) + ' times');
end;
until (i >= 10); //more than or equal to
end.
Now it will only increase i when you successfully saved gold fish and it will repeat until you caught 10 or more fishes.

Runehack123

2012-10-22 01:48:32

ok thanks so heres what happened :( …
begin
ActivateClient;
StartGame;
repeat
if SaveGoldfish then
begin
Inc(i);
Writeln('We have saved a Goldfish ' + IntToStr(i) + ' times');
end;
until(i>=10);
end.

debug:
Compiled successfully in 31 ms.
We have saved a Goldfish 1 times
We have saved a Goldfish 2 times
We have saved a Goldfish 3 times
We have saved a Goldfish 4 times
We have saved a Goldfish 5 times
We have saved a Goldfish 6 times
We have saved a Goldfish 7 times
We have saved a Goldfish 8 times
We have saved a Goldfish 9 times
We have saved a Goldfish 10 times
Successfully executed.

What happened:
i didnt start increasing until my first goldfish was dragged and dropped.
Then it rapidly increased to 8 then stopped.
Then it increased to 10 even before the next goldfish was scrolled over.
Then it was over.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
begin
MoveMouse(x, y);
HoldMouse(x, y, 1);
MoveMouse(306, 103);
Wait(50);
ReleaseMouse (306, 103, 1);
Result:= True;
end;

i took longer to increase when wait was at 2000ms, but now that I decreased it to
50ms it just shoots up so it only seems to relate to the timer and not the actual amount
of fish saved.
How do I get the result to come out true only when a fish is saved?
i starts shooting up as soon as it finds the colour, but I only want it to increase by 1 every time
a goldfish is dropped in a bowl.

Please help! :) I really appreciate any advice on this.