# Pascal script set var question

**URL:** https://talk.remobjects.com/t/pascal-script-set-var-question/10074
**Category:** Pascal Script
**Created:** [July 15, 2016, 2:55pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074 "2016-07-15T14:55:16Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 12:06pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/1 "2016-07-15T12:06:19Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 15, 2016, 12:19pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/2 "2016-07-15T12:19:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 2:49pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/3 "2016-07-15T14:49:42Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 15, 2016, 2:55pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/4 "2016-07-15T14:55:16Z")

</div>

> [@New in PS. Simple questions](https://talk.remobjects.com/t/new-in-ps-simple-questions/7868/11):
>
> 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.

Yes it does:

> <https://github.com/remobjects/pascalscript/blob/master/Source/uPSComponent.pas#L240>

(And hasn’t changed in like 12 years)

And SetVarToInstance is just a shortcut to:

```auto
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);

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 3:06pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/5 "2016-07-15T15:06:58Z")

</div>

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???

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 15, 2016, 3:11pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/6 "2016-07-15T15:11:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 15, 2016, 3:11pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/7 "2016-07-15T15:11:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 3:16pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/8 "2016-07-15T15:16:57Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 15, 2016, 3:18pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/9 "2016-07-15T15:18:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 4:23pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/10 "2016-07-15T16:23:26Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 5:00pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/11 "2016-07-15T17:00:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 5:08pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/12 "2016-07-15T17:08:03Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 15, 2016, 5:36pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/13 "2016-07-15T17:36:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 5:55pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/14 "2016-07-15T17:55:43Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 15, 2016, 5:57pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/15 "2016-07-15T17:57:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 6:06pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/16 "2016-07-15T18:06:49Z")

</div>

Really complex.

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 15, 2016, 6:35pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/17 "2016-07-15T18:35:49Z")

</div>

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)))))

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 15, 2016, 6:41pm UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/18 "2016-07-15T18:41:07Z")

</div>

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

```auto
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

```auto

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;

```

---

<div class="post-metadata">

### Author: ![Juan\_C.Cilleruelo](https://talk.remobjects.com/user_avatar/talk.remobjects.com/juan_c.cilleruelo/32/2989_2.png) [@Juan\_C.Cilleruelo](https://talk.remobjects.com/u/Juan_C.Cilleruelo)
#### Post date: [July 18, 2016, 9:48am UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/19 "2016-07-18T09:48:33Z")

</div>

“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!

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 18, 2016, 9:50am UTC](https://talk.remobjects.com/t/pascal-script-set-var-question/10074/20 "2016-07-18T09:50:29Z")

</div>

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.

[Next page](https://talk.remobjects.com/t/pascal-script-set-var-question/10074.md?page=2)
