Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Calling script functions dynamically from several running LMDScriptControl instances

Hi,

I have tried to write a small application with two LMDEditView, two LMDEditDocument and two LMDScriptControls on a form and a button for execute script from LMDDScriptControl1

Here is the code which will be executed pressing button:

procedure TForm1.ToolButton1Click(Sender: TObject);
begin
  // Prepare first script for execution
  LMDScriptControl1.Active := False;
  LMDScriptControl1.ClearObjects;
  LMDScriptControl1.Source.Text := LMDEditDocument1.Lines.Text;
  LMDScriptControl1.Prepare;

  // Prepare second script for execution
  LMDScriptControl2.Active := False;
  LMDScriptControl2.ClearObjects;
  LMDScriptControl2.Source.Text := LMDEditDocument2.Lines.Text;
  LMDScriptControl2.Prepare;

  // try to make script B visible to script A
  LMDScriptControl1.AddObject('FuncB', LMDScriptControl2.Script, True);

  // try to make script A visible to script B
  LMDScriptControl2.AddObject('FuncA', LMDScriptControl1.Script, True);

  LMDScriptControl1.Open;
  LMDScriptControl2.Open;

  // Execute 'FuncA' which contains call to 'FuncB'
  LMDScriptControl1.RunProc('FuncA');
end;

Here is the script of LMDEditView1:

procedure FuncA;
begin
        Showmessage('Hello from FuncA');
        FuncB;
end;

Here is the scrupt of LMDEditView2 which should be called from FuncA:

procedure FuncB;
begin
        Showmessage('Hello from B');
end;

However, there is no exception thrown when execute and it seems FuncB will be called from FuncA but there is no dialog box shown, only if I copy FuncB to LMDEditView1.
Currently it's nmot clear for me, how to make a second script visible to a running LMDScriptControl dynamically.

Please advice, thank you!

Comments

  • 2 Comments sorted by Votes Date Added
  • edited March 2 Posts: 3Vote Up0Vote Down
    Found the solution by myself. My code example is correct and I had only to change the script as follow:

    procedure FuncA;
    begin
            Showmessage('Hello from FuncA');
            FuncB.FuncB;
    end;

    To make it more clear, I have changed the Delphi code to this and marked the general changes bold:

    procedure TForm1.ToolButton1Click(Sender: TObject);
    var
      state: TLMDScriptControlState;
    begin
      // Prepare first script for execution
      LMDScriptControl1.Active := False;
      LMDScriptControl1.ClearObjects;
      LMDScriptControl1.Source.Text := LMDEditDocument1.Lines.Text;
      LMDScriptControl1.Prepare;

      // Prepare second script for execution
      LMDScriptControl2.Active := False;
      LMDScriptControl2.ClearObjects;
      LMDScriptControl2.Source.Text := LMDEditDocument2.Lines.Text;
      LMDScriptControl2.Prepare;
      state := LMDScriptControl1.State;


      // try to make script B visible to script A
      LMDScriptControl1.AddObject('UnitB', LMDScriptControl2.CodeObject, True); // UnitB is used as namespace

      // try to make script A visible to script B
      LMDScriptControl2.AddObject('UnitA', LMDScriptControl1.CodeObject, True); // UnitA is used as namespace

      LMDScriptControl1.Open;
      LMDScriptControl2.Open;

      // Execute 'FuncA' which contains call to 'FuncB'
      LMDScriptControl1.RunProc('FuncA');
    end;

    After change my Delphi code as shown above, my script's need to be changed like this:
    You can use Namespace indicator or simply call function from other unit when name is unique!

    Script of LMDEditView1:

    procedure FuncA;
    begin
            Showmessage('Hello from FuncA');
            UnitB.FuncB; // Call FuncB which is implemented in UnitB
            // FuncB; // This call also works fine after change AddObject above using Namespaces
    end;

    Script of LMDEditView2:

    procedure FuncB;
    begin
            Showmessage('Hello from FuncB');
            UnitA.FuncA; // Call FuncA which is implemented in UnitA
            // FuncA; // This call also works fine after change AddObject above using Namespaces
    end;

    This will work fine, both scripts can call each other, but remember code above will result in an endless loop :-) but works fine!

  • Many thanks for your feedback!!
Sign In or Register to comment.