# Read variables set by assignment

**URL:** <https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603>\
**Category:** Java\
**Created:** [February 7, 2018, 2:43pm UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603 "2018-02-07T14:43:36Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon24768507](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon24768507](https://forum.universal-robots.com/u/anon24768507)\
**Post date:** [February 7, 2018, 2:43pm UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/1 "2018-02-07T14:43:36Z")

</div>

Hello everyone. I want to create a simple popup during runtime that informs the operator what values a few key variables are set to, so they can stop the program if they are set incorrectly. The variables are set using the built in assignment function. Is it possible to implement this feature using a URCap directly?  
I know how to make the popup, but fetching the values of variables turned out to be more difficult than expected. There is a large document explaining how to make new assignments from Java, but it fails to mention reading those variables again. I thought I could just do model.get(varName,""), but this is only capable of getting variables set with model.set, not those made through regular assignment.  
I’m sure this points to a fundamental misunderstanding of mine, but I guess not all variables are created equal.

---

<div class="post-metadata">

**Author:** ![anon91082083](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@anon91082083](https://forum.universal-robots.com/u/anon91082083)\
**Post date:** [February 8, 2018, 4:40am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/2 "2018-02-08T04:40:36Z")

</div>

I have also struggled with this same issue (reading urscript variables via Java). Right now I am looking into setting the value to a general purpose register with urscript and attempting to read it…somehow…? not really sure. I just created a similar post here: [Get variable/register value from Java](http://forum.universal-robots.com/t/get-variable-register-value-from-java/1604)

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [February 8, 2018, 8:40am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/3 "2018-02-08T08:40:08Z")

</div>

The values stored in the DataModel have no direct affect, to variables present in the program.  
The DataModel values, you access by e.g. `model.get()` and `model.set()` are the settings and properties of your node, and may not be related to a Variable present in the program logic and e.g. showing up in the “Variables tab”.

If a variable is present in the program VariableModel (i.e. also selectable from the Variables dropdown in Assignment or the Expression Editor) then you can access this collection by `Collection<Variable> variables = api.getVariableModel.getAll()`.  
Now if you wanted to use one of these variables in the URScript, e.g. to show in a URScript based popup, you should use the ScriptWriter to get the resolved name of the variable object. This is due to the fact that the Variable object you have selected, may have been renamed since you selected it.  
If I have the variable `Variable myVariable` from the VariableModel collection, then to use this in URScript, I should do something like:

```auto
Variable variable = getSelectedVariable();
if (variable != null) {
	String resolvedVariableName = writer.getResolvedVariableName(variable);
	// Use the resolvedVariableName in URScript
} else {
	// Handle this null somehow
}

```

Consider checking out the two SDK examples “idletime” and “cyclecounter” which both address working with Variables.

---

<div class="post-metadata">

**Author:** ![anon24768507](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon24768507](https://forum.universal-robots.com/u/anon24768507)\
**Post date:** [February 8, 2018, 9:27am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/4 "2018-02-08T09:27:51Z")

</div>

I’m sorry, but I’m really not that experienced with java so I have some follow-up questions if that’s alright.  
When I have the collection of variables, and the resolved name of the variable, how do I get the value of said variable? All the examples I found use the entire list, and displays that with some sorting, but I don’t want all the variables, just the few that are set by a prompt to the operator.  
int varVal = variableCollection.myResolvedVarName.value(); // Clearly incorrect, but you catch my drift.  
Also, since getting the resolved variable name requires the scriptWriter, is it only callable within the generateScript-function or can I pass the writer to a subfunction?  
Kind regards.

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [February 8, 2018, 10:46am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/5 "2018-02-08T10:46:19Z")

</div>

It is not necessarily possible, to get the value of a Variable in Java.  
The value is first actually assigned to a variable at runtime, hence it is known in URScript.

E.g. in this Program, it is not possible to get a meaningful resolved value of “SomeVariable” when the program is not running.  
However when a program starts running, based on the time, “SomeVariable” will at any time have a unique value.  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/1X/c5a783026fe3604fdfe5bf0b25612a666c06f036.png)

---

<div class="post-metadata">

**Author:** ![anon24768507](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon24768507](https://forum.universal-robots.com/u/anon24768507)\
**Post date:** [February 8, 2018, 12:24pm UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/6 "2018-02-08T12:24:23Z")

</div>

I want the value of the variable at the time the custom program node of the URCap is run. So, say I put my URCap node right before thread 1 in your sample program there, I want the value 5. Is that not possible?  
It was my understanding that the function generateScript was run when PolyScope tries to run my program node, thus the value of the variable read could be different every time, depending on what assignments were made before it, but it would still be the correct value. Just like any URScript that uses that variable can read the current value and use it in expressions.

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [February 8, 2018, 2:07pm UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/7 "2018-02-08T14:07:31Z")

</div>

What about:

```auto
@Override 
public void generateScript(ScriptWriter writer):
   Variable variable = getSelectedVariable();
   if (variable != null) {
   	String resolvedVariableName = writer.getResolvedVariableName(variable);
   	writer.ifCondition( resolvedVariableName + "> 10"):
   	   writer.appendLine( "popup(" + resolvedVariableName + ",\"" + resolvedVariableName + " is larger than 10\")");
   	   writer.appendLine("halt");
   	writer.end;
   } else {
   	// Handle this null somehow
  }
}

```

This will show a popup and halt the program.  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/1X/06f419498b46d82c902a3f38f93ed0fe5f4440d9.png)

---

<div class="post-metadata">

**Author:** ![anon42905246](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon42905246](https://forum.universal-robots.com/u/anon42905246)\
**Post date:** [February 19, 2018, 7:39am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/8 "2018-02-19T07:39:05Z")

</div>

> [@jbm](#):
>
> If a variable is present in the program VariableModel (i.e. also selectable from the Variables dropdown in Assignment or the Expression Editor) then you can access this collection by Collection\<Variable\> variables = api.getVariableModel.getAll().

I can not find getVariable() function in my Eclipse.  
 ![where_is_getVariable](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/1X/b38651ff7747ea697a615ca60944c9265f8806f8.PNG)  
I searched through Maven dependencies → api and there is nothing about variables. Do I have an old version of SDK (1.1.10)?

EDIT: Looks like I do. I changed pom.xml to be dependent on the same version of API as cyclecounter (1.2.56). Does it mean that I will not be able to run it on older controller than 3.4? I created my project with newURCap.sh set to 3.4 originally.

---

<div class="post-metadata">

**Author:** ![anon42905246](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon42905246](https://forum.universal-robots.com/u/anon42905246)\
**Post date:** [February 19, 2018, 8:27am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/9 "2018-02-19T08:27:06Z")

</div>

Next step. I got this far. How do I set the value of variable?

 ![how_do_i_set_value_of_variable](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/1X/872b795f471ce1b1048b0822f6cf15ef973a85e8.PNG)

```
@Override
public void closeView() {
	// TODO Auto-generated method stub
	Collection<Variable> x = this.api.getVariableModel().get(new Filter<Variable>() {
		@Override
		public boolean accept(Variable element) {
			return element.getDisplayName().equals("MY_VARIABLE_NAME");
		}
	});		
	x.toArray()[0]. _____ now_what?
}
```

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [February 19, 2018, 8:50am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/10 "2018-02-19T08:50:16Z")

</div>

You can assign a value to a variable using the ScriptWriter.  
The Variable has no actual variable, when no program is running, so you can create the assignment in the generateScript method, using the resolved name of the variable.

> [@jbm](#):
>
> Variable variable = getSelectedVariable();  
> if (variable != null) {  
> String resolvedVariableName = writer.getResolvedVariableName(variable);  
> // Use the resolvedVariableName in URScript  
> } else {  
> // Handle this null somehow  
> }

You can also save the Variable object directly to the DataModel, if you wish to store it.

---

<div class="post-metadata">

**Author:** ![anon42905246](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon42905246](https://forum.universal-robots.com/u/anon42905246)\
**Post date:** [February 19, 2018, 8:55am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/11 "2018-02-19T08:55:45Z")

</div>

I started another thread, because this is one is about getting the value of variable:

> [@Set installation variable and create it if it does not exist](http://forum.universal-robots.com/t/set-installation-variable-and-create-it-if-it-does-not-exist/1641):
>
> Continuing the discussion from [Read variables set by assignment](http://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/9): Correct me, if my reasoning process is wrong. InstallationNode creates a section of code in script file, when the file is created or changed. My intention is to create a URCap, where I can set up some configuration options. These options, however, will not be updated, until I change the program and click “Save” icon. How would I achieve the desired effect, i.e. user changes option in InstallationNode and runs the program without …

I want to get access to Installation variable, which will exist, when no program is running, or I will create it if it does not exist yet. Doing it in ScriptWriter does not have effect, if program is not touched (created or modified).

---

<div class="post-metadata">

**Author:** ![anon46802796](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@anon46802796](https://forum.universal-robots.com/u/anon46802796)\
**Post date:** [April 10, 2018, 6:47am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/12 "2018-04-10T06:47:22Z")

</div>

I’m having some problems when running my URCap using _getVariableModel()_ and _getRobotModel()_. I always got the _NoClassDefFoundError_ exception at run time, both on Ursim 3.5.3 and Controller Image 3.5.3.

I run the built _scriptwrapper_ example and it works, when I do the same in my example I always get exceptions. I checked in the bundle folder, and the api-1.2.56.jar is present, and is the same I use when building.

There is something I’m doing wrong?

Best regards,

Matteo Timossi @anon46802796

---

<div class="post-metadata">

**Author:** ![anon63859582](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@anon63859582](https://forum.universal-robots.com/u/anon63859582)\
**Post date:** [April 13, 2018, 10:56am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/13 "2018-04-13T10:56:43Z")

</div>

Maybe this is off this specific topic.

I have a custom porgram node with 2 input textfields that on change assigns a Datamodel variabel.  
This node, will have (& create by push of button like “pickorPlace” example) a tree containing other custom nodes.  
These nodes then have to make use of the to Datamodel variables to use them in the generateScript method - the 2 variables hold IP/port for an external server.

I know this is possible with an installation node, but is there an equal method to:  
private HelloWorldInstallationNodeContribution getInstallation() {  
return api.getInstallationNode(HelloWorldInstallationNodeContribution.class);  
}  
(From “HelloWorld”)?

Thanks

---

<div class="post-metadata">

**Author:** ![anon63859582](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@anon63859582](https://forum.universal-robots.com/u/anon63859582)\
**Post date:** [April 13, 2018, 11:33am UTC](https://forum.universal-robots.com/t/read-variables-set-by-assignment/1603/14 "2018-04-13T11:33:19Z")

</div>

SOLVED:

I accedently re-assigned the IP & variable in my URscript to default values.
