SRL Archive - RS3 Help

[LAPE] WaitFunc in Lape - is it possible ?


bg5

2014-06-21 18:47:08

Hello,
I used to use a lot of "WaitForSomething" functions in my scripts to synchronize tasks. In PascalScript I had for this purpose functions: WaitFunc() and even better WaitFuncEx(). Now I'm switching to Lape and I'm looking for equivalent solution,but I can't see any. Is it possible in Lape to pass one function as an argument to second function and run it inside her?

Frement

2014-06-21 19:20:12

VariantInvoke function is the equivalent of CallProc, you can use it to create WaitFunc.

The Mayor

2014-06-21 19:57:29

There is waitFunc in SRL6


function waitFunc(Func: function: Boolean; WaitPerLoop, MaxTime: Integer): Boolean;
var
T: Integer;
begin
T := GetSystemTime() + MaxTime;
while (GetSystemTime() < T) do
begin
if (Func()) then
begin
Result := True;
Exit;
end;
Wait(WaitPerLoop);
end;
end;

bg5

2014-06-21 20:02:05

Thanks, works :)


function WaitFunc(Func:string; Args : array of Variant; WaitTime,MaxTime : integer): boolean;
var t: integer;
begin
t := GetTimeRunning();
while (t+MaxTime > GetTimeRunning()) do
begin
Result := variantInvoke(Func,Args);
if Result then exit;
Wait(WaitTime);
end;
end;



@EDIT

I caught a vein:


function WaitFuncNumber(Func:string; Args : array of Variant; WaitTime,MaxTime:integer; Relation:string;Number:integer ): boolean;
var t,b: integer;
begin
t := GetTimeRunning();
while (t+MaxTime > GetTimeRunning()) do
begin
case Relation of
'=': Result := variantInvoke(Func,Args) = Number;
'<>': Result := variantInvoke(Func,Args) <> Number;
'>': Result := variantInvoke(Func,Args) > Number;
'<': Result := variantInvoke(Func,Args) < Number;
'<=': Result := variantInvoke(Func,Args) <= Number;
'>=': Result := variantInvoke(Func,Args) >= Number;
end;
if Result then exit;
Wait(WaitTime);
end;
end;

That waits till output of the function achieves defined relation, for example: you can wait till number of red pixels in some box >= 100.

Example:


program new;
function W(a:integer) : integer;
begin
Result := a;
end;


function WaitFuncNumber(Func:string; Args : array of Variant; WaitTime,MaxTime:integer; Relation:string;Number:integer ): boolean;
var t,b: integer;
begin
t := GetTimeRunning;
while (t+MaxTime > GetTimeRunning) do
begin
case Relation of
'=': Result := variantInvoke(Func,Args) = Number;
'<>': Result := variantInvoke(Func,Args) <> Number;
'>': Result := variantInvoke(Func,Args) > Number;
'<': Result := variantInvoke(Func,Args) < Number;
'<=': Result := variantInvoke(Func,Args) <= Number;
'>=': Result := variantInvoke(Func,Args) >= Number;
end;
if Result then exit;
Wait(WaitTime);
end;
end;

var a:integer;
begin
a := 4;
writeln ( WaitFuncNumber('W',[a],10,2000,'>',10) );
end.

bonsai

2014-06-21 20:16:37

There's a new timer type in srl6 too.

var t: TTimeMarker;
...
t.start();
repeat
wait(250);
until (t.getTime() > 10000);