SRL Archive - OSR Advanced Scripting Tutorials

Learn Function Senders


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.

Lalaji

2007-12-09 16:01:01

I seriously didnt know you could do that…
Its like i am falling behind the SRL community…:(

mastaraymond

2007-12-09 17:03:27

Yeah they are pretty cool..

It took me a long time to find out that Functions without parameters, need to be called with () behind it..


Only problem is, i can't find a use for it ^^

Boreas

2007-12-09 22:24:50

Here is something you can do:

RS players have different dropping orders. That is, some people drop from left to right, top to bottom, like reading English, some drop top to bottom, left to right, like reading Ancient Egyptian, some do the evens then odds, some skip around in a sequence, some randomly. You could make a function DoSomethingToInventory(OrderStyle, Something), which has a case for the different dropping styles. And Something calls a procedure. That procedure can be dropping, or using (burying), or clicking a well and then using, or anything else. That way, you can do any action to the items in the inventory, using any dropping style, but only have to code the dropping styles into one function.

SKy Scripter

2007-12-09 22:28:38

From: Boreas
Here is something you can do:

RS players have different dropping orders. That is, some people drop from left to right, top to bottom, like reading English, some drop top to bottom, left to right, like reading Ancient Egyptian, some do the evens then odds, some skip around in a sequence, some randomly. You could make a function DoSomethingToInventory(OrderStyle, Something), which has a case for the different dropping styles. And Something calls a procedure. That procedure can be dropping, or using (burying), or clicking a well and then using, or anything else. That way, you can do any action to the items in the inventory, using any dropping style, but only have to code the dropping styles into one function.


Yea, even pwnaz0r mentioned sothing like
Players[CurrentPlayer].OnDie := @Procedure;

things like That XD

so it can be useful…

pwnaz0r

2007-12-09 22:39:24

did someone say my name :D (it seems I always view a thread just as someone posts my name…).

Yeah, mostly event handling is what I was thinking they could be useful for.

R0b0t1

2007-12-10 17:39:43

I second that, because what could you possibly want to use function pointers for besides that?


Or the calling of miscellaneous functions and procedures…


@SKy - Where have you been? Buttons use the @ sign you know. That lead me into the type thing.

mastaraymond

2007-12-10 17:50:37

From: R0b0t1
I second that, because what could you possibly want to use function pointers for besides that?


Or the calling of miscellaneous functions and procedures…


@SKy - Where have you been? Buttons use the @ sign you know. That lead me into the type thing.


Pointers in scar?

Bobarkinator

2007-12-13 10:27:58

Yay thanks :)

I got it a lot better the second time I read it.

R0b0t1

2007-12-15 23:13:52

Pointers in scar? I dun think so… But scar does have the ___Pointer type.


But they have Variants… Like pointers…

mastaraymond

2007-12-16 05:07:15

From: R0b0t1
Pointers in scar? I dun think so… But scar does have the ___Pointer type.


But they have Variants… Like pointers…


Scar doesn't!

program New;
var
J : Array[0..5] of TPoint;
D : Variant;

begin
D := @J[0].x;
end.

Or try the ^.. Does NOT work..

Nava2

2008-10-02 14:13:33

A use could be debug.. but this is dead so yeah :)