Forward initiated record from Delphi to the Script Engine

Hi,

I’m currently try to forward an initiated record from delphi to the script engine. In an minimal example I have created a record

TRecTest = record
  Value: Double;
end;

I put this record in an Unit and created a plugin with “imp.exe”.
In my delphi source code I do something like

 ...
 RecTest: TRecTest;
 ...
 RecTest.Value := 5;
 ... 

And in the OnCompile Event of the Scriptengine I register a global variable such as

FPScript.AddRegisteredVariable('RecTest', 'TRecTest');

Now in the OnExecute event I try to assign ‘RecTest’ variable of my delphi code to the ‘RecTest’ variable in the scriptengine such as

Sender.SetVarToInstance('RecTest', RecTest)

But when I access the Value property of ‘RecTest’ in the scriptengine I receive ‘0.0’ and not ‘5’!

Can anybody help me with this issue?

Thanks in advance

Florian

can you attach your sample, pls? by other hand, you can send it to support@

Here a fractional example:

TNestedClass = class
  private
    FValue: Double;
  public
    property Value: Double read FValue write FValue;

    constructor Create(AValue: Double);
  end;

  TRecNested = record
    Value: Double;
  end;

  TParentClass = class
  private
    FNested: TNestedClass;
    FRecNested: TRecNested;
  public
    property Nested: TNestedClass read FNested write FNested;
    property RecNested: TRecNested read FRecNested write FRecNested;

    constructor Create;
  end;

These classes and records I have assigned to the PascalScript via the “imp.exe”:

PSPlugin := TPSImport_uTestClass.Create(Self);
TPSPluginItem(Self.FPScript.Plugins.Add).Plugin := PSPlugin;

Now in Delphi I do the following

AParentClass.RecNested.Value := 5;
Self.FPScript.SetVarToInstance('AParent', AParentClass);

When I try to access AParent.RecNested.Value in the PascalScript engine I got the following error:

Compiler : [Error] (4:26): Unknown identifier 'VALUE'

But I can access AParent.Nested.Value in PascalScript w/o an compiler error…

Thanks for your support!

I don’t know about the “Unknown identifier” error, though on the face of it this looks like the TRecTest type hasn’t been properly described to the PascalScript engine such that it simply doesn’t know about the “Value” field in that context.

But with respect to the changed value not appearing to take effect, I think this could be the result of the fact that tecords are value types, not reference types. So in your script when you write:

 obj.RecNested

You are effectively creating a copy of the current fRecNested member record. When you then modify a field in that record you are modifying a copy of fRecNested, not fRecNested itself.

i.e. this code:

 obj.RecNested.Value := 5;

is equivalent to:

 v := obj.RecNested;
 v.Value := 5;

To modify the fRecNested member record using a write property of that record type, you must provide a new, complete record value:

 var
   v: TRecNested;

    ...

  v.Value := 5;
  obj.RecNested := v;   

The easiest way to avoid this confusion (and the potential for such mistakes), is to make the record member property read only and provide an explicit SetXXX() method, so that it is clear that a new record value must be provided. Or, if only certain fields in the record need to be modifiable, provide specific SetXXX() methods for those fields:

obj.SetRecNested( someNewRecord );   // updates fRecNested
obj.SetRecNestedValue( 5 );          // updates fRecNested.Value

Hope that helps.