SRL Archive - News and General

Convinience Function for Multiple Attempts


Tniffoc

2011-01-22 03:24:44

I found that this proved to be really useful when writing my last script. Shown below are the both the function, and a useful application of it.

Just thought this was a cool idea and it would be fun to share!


function AttemptFunction(Func: function():Boolean; Attempts, Intermission: Integer): Boolean;
var
i: Integer;
begin
for i:=1 to Attempts do
begin
Result := Func();
if Result then
Break;
Wait(Intermission);
end;
end;

// Somewhere later in your script...
AttemptFunction(@ClickAnvil, 10, 1000);
The example shown above will attempt to click an anvil 10 times, waiting 1000 miliseconds between each attempt. It will return whether or not it was successful in clicking the anvil.

One limitation of this is that you cannot pass arguments to the function pointer that is passed to AttemptFunction. You can get around this by wrapping functions in other functions, but I see this as more work than is necessary, and I only used this method once. For example, if you wanted to iterate on opening the Varrock West Bank…

function SrlOpenBank: Boolean;
begin
OpenBank('vwb', false, false);
end;
// Somewhere later in your script...
AttemptFunction(@SrlOpenBank, 5, 2500);
I find the wrapping method to be more bloating than its worth, but for functions that take no parameters, then this can save you a couple lines of code and also make the code easier to follow around instead of having a million loops all over the place.

Feels good to be back around here!

Sex

2011-01-22 06:03:08

function AttemptFunction(Func : string; Parameters : TVariantArray; DesiredResult : variant; Attempts, Intermission : integer) : Variant;
var
i : integer;
begin
for i := 1 to Attempts do
begin
try
result := CallProc(Func, Parameters);
except end;
if VarType(result) <> varType(DesiredResult) then
begin
if VarType(result) = varNull then
begin
DesiredResult := True;
result := DesiredResult
end else
begin
Writeln('Error: Type mismatch.');
exit;
end;
end;
if result = DesiredResult then
break;
Wait(Intermission);
if i = Attempts then
result := False;
end;
end;
Accepts parameters. You choose desired result. If the types mismatch, it exits (for example, you input 'runescape' as desiredresult but the function returns a boolean, it will throw the error). If it is a procedure with no result, the function returns true. I was bored, so I made that XD.

Gu3st...

2011-01-22 07:46:54

Sex's function could be AttemptFunctionEx() which AttemptFunction() relies on

Sex

2011-01-22 07:55:47

function AttemptFunctionEx(Func : string; Parameters : TVariantArray; DesiredResult : variant; Attempts, Intermission : integer) : Variant;
var
i : integer;
begin
for i := 1 to Attempts do
begin
try
result := CallProc(Func, Parameters);
except end;
if VarType(result) <> varType(DesiredResult) then
begin
if VarType(result) = varNull then
begin
DesiredResult := True;
result := DesiredResult
end else
begin
Writeln('Error: Type mismatch.');
exit;
end;
end;
if result = DesiredResult then
break;
Wait(Intermission);
if i = Attempts then
result := False;
end;
end;

function AttemptFunction(Func : string; Attempts, Intermission : integer) : boolean;
begin
result := AttemptFunctionEx(Func, [], True, Attempts, Intermission);
end;
It is slightly different of course, instead of a pointer its the function name :p.

Bad Boy JH

2011-01-24 00:24:47

Nice! this would be an incredible addition and would seriously make life easier when scripting…Even though I am pretty much taking a break from scripting…

Sir R. M8gic1an

2011-01-24 00:35:54

Why is this in news…?

This is a great example of a Developer's Tool.

~RM

Tniffoc

2011-01-24 22:48:11

From: Sir R. Magician
Why is this in news…?

This is a great example of a Developer's Tool.

~RM


I just put it in news because I didn't think it really belonged in SRL, but if you have a place for it, the please, move the thread!

If I can move it, I still can't find the button, and yes, AttemptFunctionEx and AttemptFunction would be great. I had no idea CallProc even existed.

Might want to clean up the caps to make calls to VarType consistent before adding though!

Sir R. M8gic1an

2011-01-24 22:53:33

Meh, I'll leave that for someone else, I'll move this thread in morning tho :)

Flight

2011-01-24 23:17:26

Yeah this is a nice bit of code, I think I can use this in a couple of my beta scripts. Nice job!

BTW, maybe this should go in a Development section or something? Maybe added to Scar and Simba itself as a core function.

Sex

2011-01-25 00:40:14

From: Tniffoc
I just put it in news because I didn't think it really belonged in SRL, but if you have a place for it, the please, move the thread!

If I can move it, I still can't find the button, and yes, AttemptFunctionEx and AttemptFunction would be great. I had no idea CallProc even existed.

Might want to clean up the caps to make calls to VarType consistent before adding though!

It was added to Simba at my request a long while back :p.

Tniffoc

2011-01-25 01:04:09

From: Sex
It was added to Simba at my request a long while back :p.


AttemptFunction or VarType?

Sex

2011-01-25 01:04:54

From: Tniffoc
AttemptFunction or VarType?

CallProc :).

Tniffoc

2011-01-25 02:19:52

From: Flight
Yeah this is a nice bit of code, I think I can use this in a couple of my beta scripts. Nice job!

BTW, maybe this should go in a Development section or something? Maybe added to Scar and Simba itself as a core function.


RM is going to move it in a bit, if you read the post :D.

Wanted

2011-09-26 01:48:21

Here is a simplified copy of this

function WaitFuncEx(Func: string; Parameters: TVariantArray; DesiredResult: Variant; MinWait, MaxWait, MinTotalWait, MaxTotalWait: LongInt): Variant;
var
T: LongInt;
begin
Result := True;
T := GetSystemTime + RandomRange(MinTotalWait, MaxTotalWait);
while (GetSystemTime < T) do
if (CallProc(Func, Parameters) = DesiredResult) then
Exit
else
Wait(RandomRange(MinWait, MaxWait));
Result := False;
end;

However I have not been able to get around the limitation of having functions with variables in the parameter coming up with a mismatch.

For example

if you tried to use FindColor the var X, Y would cause a execution error type mismatch. Ignoring it causes a not enough parameters error.

Edit:

Test script

function WaitFuncEx(Func: string; Parameters: TVariantArray; DesiredResult: Variant; MinWait, MaxWait, MinTotalWait, MaxTotalWait: LongInt): Variant;
var
T: LongInt;
begin
Result := True;
T := GetSystemTime + RandomRange(MinTotalWait, MaxTotalWait);
while (GetSystemTime < T) do
if (CallProc(Func, Parameters) = DesiredResult) then
Exit
else
Wait(RandomRange(MinWait, MaxWait));
Result := False;
end;

var
P: TPoint;

begin
FindColor(P.X, P.y, 12312554, 668, 15, 670, 17);
WaitFuncEx('FindColor', [P.X, P.Y, 12312554, 668, 15, 670, 17], True, 100, 250, 90000, 120000);
end.

simba Error: Exception: Invalid type cast at line 8

scar Execution Error Line 8 invalid typecast