SKy Scripter
2007-12-09 15:50:56
Function Senders
Ok, Well i knew about function senders. however, i wasnt aware they were in SCAR (meaning you could create the type), luckily BenLand cleared that up..
In this tutorial i will explain how to use them… and the uses for them :)
Ok, first off you must have the Type… remember you can only send the procedure that matches the type..
type
(* you can have simple types like this ect.. *)
TBooleanFunction = function: Boolean;
(* or you can have parameters in your type *)
TParamFunction = function (S: string): string;
Ok now you can see that you can make any type of function.. also they can
be procedures like so
type
TProcedure = procedure (); // Just a Normal procedure
TParamsProcedure = procedure (S: string); // A Procedure with the (S : string) params
Put Them to use:
once you Declare the type you want. you must make your Sender..
For example: ill use this..
type
Tfunctionboolean = function: Boolean;
Now We will put it to use
type
Tfunctionboolean = function: Boolean;
function Findit(func : TFunctionBoolean): Boolean;
begin
Result := func();
end;
function IsMButtonDown: Boolean;
begin
Result := IsMouseButtonDown(True) or IsMouseButtonDown(False);
end;
begin
Writeln(BoolToStr(Findit(@IsMButtonDown)));
end.
Another Example is.. it might be easier to get it this way..
type
Tfunctionboolean = function: Boolean;
function Findit(func : TFunctionBoolean): Boolean;
begin
Result := func();
end;
function IsMButtonDown: Boolean;
begin
Result := IsMouseButtonDown(True) or IsMouseButtonDown(False);
end;
var t: Tfunctionboolean;
s: boolean;
begin
t := @IsMButtonDown;
s := FindIt(T);
Writeln(BoolToStr(s));
end.Why is it useful well, just look at forms….
You can do things like
Canvas.OnClick := @TheProcedure
there really isnt much to explain its very easy… just make things like the examples above.