Pascal script set var question

Can you point, please, the exact example in which is uses AddVariable? Please!!

I’ve wasted a lot of time searching the example. I think this example does not exists.

What I need is. From delphi, create a variable inside the script and set a value.

After execute the script, I need recover the value of this Variable.

Really I need to do this with a group of near of 20 variables.

Thank you!

The ide sample does exactly this. It registers a “self” variable and later sets it.

These are instances of classes (objects):

procedure Teditor.ceCompile(Sender: TPSScript);
begin
Sender.AddMethod(Self, @TEditor.Writeln, ‘procedure writeln(s: string)’);
Sender.AddMethod(Self, @TEditor.Readln, ‘procedure readln(var s: string)’);
Sender.AddRegisteredVariable(‘Self’, ‘TForm’);
Sender.AddRegisteredVariable(‘Application’, ‘TApplication’);
end

And the instruction used is AddRegisteredVariable, not AddVariable))))

These are assignments of Objects:
procedure Teditor.ceExecute(Sender: TPSScript);
begin
ce.SetVarToInstance(‘SELF’, Self);
ce.SetVarToInstance(‘APPLICATION’, Application);
Caption := STR_FORM_TITLE_RUNNING;
end;

And the instruction is CSetVarToInstance not VSetInt nor some similar.

Let me lought about this thread, but… as complicated can be get as response something concrete?

A concrete example about how to insert/create from Delphi source a variable of a single type (Integer, string, Extended, etc.)

I need to create this variable to be used inside the script, but I have not a variable outside it.

After the code is executed, I need to recover his value and put it into a variable of my delphi code, that can have other name or can be a property of a class, that the Pascal Script do not know.

It’s possible. I’m sure. But I can’t find an example or a document entry about this.

The more near I have is in the wiki that say:
… you have to use the AddRegisteredVariable Function…

this AddRegisteredVariable function does not exists in the current version of PascalScript. Exists AddVariable, but… I can’t understand his second parameter.

An example please!!! I beg an example!!!

Thank you!

Yes it does:

(And hasn’t changed in like 12 years)

And SetVarToInstance is just a shortcut to:

var p: PIFVariant;
          p := GetVariable(VarName);
          if p <> nil then
          begin
            SetVariantToClass(p, cl);
        end;

The same can be done for integers, like:

VSetInt(GetVariable(‘VArName’), 15);

From Sample1 to Sample8 code, the Classes used are: TPSPascalCompiler and TPExec.

Is because of this that they can be an example about AddRegisteredVariable. Because this is a method of TPSScript.

Well. Need I change all the code to use TPSScript instead TPSPascalCompiled in combination with TPExec?

Or is a method to do this with this classes?

Thanks you???

Not really. TPSScript wraps TPSPascalCompiler, if you take a look at the methods in TPSScript you acn see exactly what to call on TPSPascalCompiler and TPSEec.

Not really. TPSScript wraps TPSPascalCompiler, if you take a look at the methods in TPSScript you acn see exactly what to call on TPSPascalCompiler and TPSEec.

And… this means that I can or that I can’t do this.

If I can: How should I do?

If i can’t. Do you know a work around or some form of do this with TPSPascalCompiler and TPSScript?

You can. As I just said. Look at the TPSScript.AddRegisteredVariable method to see what you need to call when using TPPascalSCompiler directly. Same for GetVariable.

Now, I’ve changed all to TPSScript.

I’m using this pair of instructions:

Script.AddRegisteredVariable(prmID, ‘string’);
VSetString(Script.GetVariable(prmID), prmValue);

The second gives me an access violation error. I think that is because it can’f find the Variable prmID.
May be that, with AddRegisteredVariable, the Variable prmID, shall exists in my code?
If yes, How can I add to the script a variable of a simple type that don’t exists my Delphi code?

Seems that FCandAdd in the first line of the method AddRegisteredVariable is False and don’t allow insert variables.
if not FCanAdd then begin Result := False; exit; end;

I’m investigating if I can’t set this to True, in any moment.

Seems that I can’t AddRegisteredVariable(… before compiling.
FCanAdd is set to true inside the Compile method of Script.

This is not so useful. Sure I’m missing something!

There’s a callback on the compiler (OnUses IIRC) it’s used from pretty much all the samples, that’s where you register variables.

When executing you can AFTER loading the bytecode from the compiler into the runtime set the variables.

Compiling the script first, the FCanAdd is set to true and AddRegisteredVariable goes to the next line of code, but…
this line FVar := FComp.AddUsedVariableN(varname, vartype); gives me an exception that says:
"This method can be only called from within the OnUses event’.

This is a problem, because the OnUses event becomes to FComp: TPSPascalCompiler; and/or FExec: TPSDebugExec;

resume: I’ve changed all the code, from TPSPascalCompiler and TPSExec, like in the examples, and now I discover that the path is through the event handlers of this components.

I’m tired. So tired!

Yes, it works through events (the same goes for the TPSPascalCompiler logic).

Really complex.

I’m using TPSScript.
How to assign an OnUses evento to it?

I know. The examples!. I can’t find an example with TPSScript)))))

Can you say me, how to do it or the exact place of an example with TPSScript?

Thank you)))))

When using TPSScript you use the oncompile and onexec events, like:

procedure TForm1.PSScriptCompile(Sender: TPSScript);
begin
  Sender.AddFunction(@MyWriteln, 'procedure Writeln(s: string);');
  Sender.AddFunction(@MyReadln, 'function Readln(question: string): string;');
  Sender.AddFunction(@ImportTest, 'function ImportTest(S1: string; s2: Longint; s3: Byte; s4: word; var s5: string): string;');
  Sender.AddRegisteredVariable('vars', 'Variant');
  Sender.AddRegisteredVariable('Application', 'TApplication');
  Sender.AddRegisteredVariable('Self', 'TForm');
  Sender.AddRegisteredVariable('Memo1', 'TMemo');
  Sender.AddRegisteredVariable('Memo2', 'TMemo');
end;

and


procedure TForm1.PSScriptExecute(Sender: TPSScript);
begin
  PSScript.SetVarToInstance('APPLICATION', Application);
  PSScript.SetVarToInstance('SELF', Self);
  PSScript.SetVarToInstance('MEMO1', Memo1);
  PSScript.SetVarToInstance('MEMO2', Memo2);
  PPSVariantVariant(PSScript.GetVariable('VARS'))^.Data := VarArrayCreate([0, 1], varShortInt)
end;


“I need to create this variable to be used inside the script, but I have not a variable outside it.”

Unless vars, all the example Instances are objects and exists outside of the Script.

Why OnCompile uses “Sender” and “OnExecute” use instead PSScript. It’s confusing.

Can I see and example with an integer value, or with an string, please?

Thank you!

Because I copy/pasted this from a sample and it accidentally had that. Both are the same thing.

In any case I don’t have an example handy for integers/strings.