TRect by adress in Event Dispatcher

Hello,

I would like to convert this event to scripter:

  TOnDrawShape = procedure(const canvas: TCanvas; var rect: TRect;
    item: TShape) of object;

I have done it like this:

procedure TatNativeShapeDispatcher.__TOnDrawShape(const canvas: TCanvas; var rect: TRect;  item: TECShape);
var
  rectTemp: Trect;
  RestAdr:NativeUInt;
begin
  if DoOnExecuteEvent then
  begin
    if AssignedMethod(BeforeCall) then
      TOnDrawShape(BeforeCall)(canvas,rect,item);
    rectTemp := rect;
    if Assigned(Scripter) and (RoutineName > '') then
    begin
      RestAdr:=ObjectToVar(TRectWrapper.Create(rectTemp));
      Scripter.ExecuteSubroutine(RoutineName, [canvas,RestAdr,item]);
      rectTemp:= TRectWrapper(RestAdr).ObjToRec;
    end;
    rect := rectTemp;
    if AssignedMethod(AfterCall) then
      TOnDrawShape(AfterCall)(canvas,rect,item);
  end;
end;

Registred with :

 DefineEventAdapter(TypeInfo(TOnDrawShape), TatNativeShapeDispatcher, @TNativeShapeDispatcher.__TOnDrawShape);

But in scripter when I try to do something like:

procedure doDrawHint( canvas : TCanvas; var  r:Trect;  Item: TShape);
begin

  canvas.TextRect(r, 0, 0, 'MyText');

end;

I have an error "Can't convert a variant of type (null) to type (Double)"

With :

procedure doDrawHint( canvas : TCanvas; var r;  Item: TShape);
begin

  canvas.TextRect(r, 0, 0, 'MyText');

end;

I have an error "EInvalidCast".

same error with :

procedure TatuecNativeShapeDispatcher.__TOnDrawShape(const canvas: TCanvas; var rect: TRect;  item: TShape);
var
  rectTemp: TRectWrapper;
begin
  if DoOnExecuteEvent then
  begin
    if AssignedMethod(BeforeCall) then
      TOnDrawShape(BeforeCall)(canvas,rect,item);
 
    if Assigned(Scripter) and (RoutineName > '') then
    begin
      rectTemp := TRectWrapper.Create(rect);
      Scripter.ExecuteSubroutine(RoutineName, [canvas,rectTemp,item].setvari);
      rect:= TRectWrapper(rectTemp).ObjToRec;
    end;
  
    if AssignedMethod(AfterCall) then
      TOnDrawShape(AfterCall)(canvas,rect,item);
  end;
end;

What I'am doing wrong ?

All seems correct at first sight. I'm afraid we need your project reproducing the issue so we can debug it here. Is it possible that you send a project that we can run and debug here?

Yes I have found the problem.
I have used :

  Scripter.DefineClassByRTTI(TECCanvas);

But DefineClassByRTTI seem to fail with record argument like Trect ?
So I have fixed it with :

  With Scripter.DefineClass(TECCanvas) do
  begin
    DefineMethod('TextRect',4,tkNone,nil,__TECCanvasTextRect,false,0,'rect: TRect; X: integer; Y: integer; Text: string');
  end;

I never know if I must use RTTI or not (with Scripter Studio Import tool) because both have it advantage and inconvenient...

Good you have solved the issue. Actually, there are two record wrappers in TMS Scripter. The "old" one, which is TRectWrapper (and all the specific ones for any other imported record, like TPointWrapper, etc.) and the "new" one which descend from TGenericRecordWrapper and is the one registered by the methods using the new RTTI. They are not compatible.

So that's why the method declared using ByRTTI was not able to understand the TRectWrapper object.