Using script functions for callback

<< Click to Display Table of Contents >>

Navigation:  Using Delphi classes and functions >

Using script functions for callback

Script functions can be used as callback functions in Delphi Code.

For example, there is a class containing list of objects, and we want to sort it from script with custom compare procedure.

 

In Delphi code we declare compare procedure:

 

function TMyList.ScriptCompare(Func: TScriptFunc;
  const E1, E2: TListElement): integer;
begin
    Result := Func.Owner.RunFunction(Func, [Object2Variant(E1), Object2Variant(E2)])
end;

 

and Sort procedure

 

procedure TMyList.SortElements(Compare: TScriptFunc; Asc: boolean);
begin
  ...
end;

 

Sort procedure is called from script code:

 

 MyList.SortElements(
   function(E1, E2: TObject); 
   begin 
    ...
   end
  true);