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!
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…
Feels good to be back around here!
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!