# Loop according to variable

**URL:** <https://forum.universal-robots.com/t/loop-according-to-variable/17653>\
**Category:** URScript\
**Created:** [November 17, 2021, 7:48am UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653 "2021-11-17T07:48:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![tommipocbp](https://avatars.discourse-cdn.com/v4/letter/t/ec9cab/32.png) [@tommipocbp](https://forum.universal-robots.com/u/tommipocbp)\
**Post date:** [November 17, 2021, 7:48am UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/1 "2021-11-17T07:48:53Z")

</div>

Hi,

This is probably very simple thing but i 'm not very familiar with script yet 🙂  
I want to make a program that loops specific lines according to operator input.  
I didn’t find the script for that from the manual.  
There is the “Loop when expression is true” but how i can make it read the operator input.

Thanks!

---

<div class="post-metadata">

**Author:** ![MikeM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mikem/32/3114_2.png) [@MikeM](https://forum.universal-robots.com/u/MikeM)\
**Post date:** [November 17, 2021, 12:35pm UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/2 "2021-11-17T12:35:37Z")

</div>

In Polyscope try adding an assignment but change it from “Expression” to “Operator” then add some text instruction for operator input before the loop. Also add a count variable

```auto
OperatorInput:=‘Number of times to loop’
Count:=0

```

Then you can add the loop

```auto
Loop Count < OperatorInput:
   // Do some stuff
   // OperatorInput:=‘Change number of times to loop // added inside in case this changes and operator needs to adjust number of times within the loop
   Count:=Count+1 // if Count is made an installation variable you can use the “SET” node to increment this value, just remember to add Count:=0 at the start of your program
   // sync() // use up remaining physical time

```

Sorry might have been a lot but urcommunity might have other better options. Hope that helps and best of luck!

---

<div class="post-metadata">

**Author:** ![eric.feldmann](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@eric.feldmann](https://forum.universal-robots.com/u/eric.feldmann)\
**Post date:** [November 17, 2021, 6:48pm UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/3 "2021-11-17T18:48:05Z")

</div>

I like to mention this whenever possible, because it is so handy. If you ever say “I can do this in Polyscope, how can I do it in script?” then this is the answer, every time!

First, just make the program in Polyscope. As @MikeM points out, you can use the Assignment node and switch it to Operator as the source

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/e/e274cbf5e879beb4b68b4eab3022dcb05632704d.png)

Now just build out the rest of the program in Polyscope. For example, I made a loop which just prints the count.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/6/6794ef98479dae43c85c1179b3890c6b7798f68f.png)

Now, for the magic. Save this program as anything you’d like. Now add a “Script” node to Polyscope, and switch it from “Line” to “File” in the top right. Then click the Edit button

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/5/5b4a9c11ae1891832d3fd41f4939c7094fc70246.png)

Click the Open button in the top left, and Open the file you just saved. What you’ll see is the exact URScript that Polyscope built behind the scenes. This means you could instead write exactly what’s here, delete your entire Polyscope program, and it will do the same thing.

![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/a/aeb1ebb1d7d871dd76ce42ededdb20903dc6cf03.png)

(The lines starting with a dollar sign $ are just labels that correspond to the line number. The actual lines of code being executed are the lines without those $)

So As you can see, in order to assign the variable “input” through the operator, it looks like Polyscope wrote  
`global input=request_integer_from_primary_client("")`

Therefore that’s the line of script you’re probably looking for as well! Hope this helps.

---

<div class="post-metadata">

**Author:** ![tommipocbp](https://avatars.discourse-cdn.com/v4/letter/t/ec9cab/32.png) [@tommipocbp](https://forum.universal-robots.com/u/tommipocbp)\
**Post date:** [November 18, 2021, 4:33am UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/4 "2021-11-18T04:33:13Z")

</div>

Hi,

Ok, so I was on the right track actually, i just missed couple small details.  
And i thought there is some more simple ways to read the operator input than use counter variables etc. like just one script line in the loop node but this seems to work well too 🙂

Thanks guys, this was helpful for the future as well!

---

<div class="post-metadata">

**Author:** ![MarcoD](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@MarcoD](https://forum.universal-robots.com/u/MarcoD)\
**Post date:** [July 31, 2024, 2:19pm UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/5 "2024-07-31T14:19:31Z")

</div>

Hello,

I found this topic about using a loop according a variable entered by an operator.  
I use the polyscope code in my program, but unfortunately it doesn’t seem to work.  
When I start the program, the screen appears to fill in the numbers, after that the pop-up appears  
with the message to stop the program or to continue, but I have to push the continue button until  
the entered numbers are correct and then it starts all over again.

It is propably very simple, but I could not find the clue yet.

Thanks!

---

<div class="post-metadata">

**Author:** ![eric.feldmann](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@eric.feldmann](https://forum.universal-robots.com/u/eric.feldmann)\
**Post date:** [July 31, 2024, 2:49pm UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/6 "2024-07-31T14:49:44Z")

</div>

Can you post a screenshot of your code? It would help us diagnose the issue.

---

<div class="post-metadata">

**Author:** ![MarcoD](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@MarcoD](https://forum.universal-robots.com/u/MarcoD)\
**Post date:** [August 1, 2024, 6:03am UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/7 "2024-08-01T06:03:56Z")

</div>

![IMG_0353](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/3/379a6e2e734abec33c01343849239f071c8547a8.jpeg)  
 ![IMG_0354](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/6/67a6f6a84b890ec453e0b65af2768d6a260d46b8.jpeg)

I hope that this would help! The program is mainly in Dutch, because that is the default setting of our UR3.  
If it is a problem I could translate the words.

---

<div class="post-metadata">

**Author:** ![eric.feldmann](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@eric.feldmann](https://forum.universal-robots.com/u/eric.feldmann)\
**Post date:** [August 1, 2024, 4:47pm UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/8 "2024-08-01T16:47:30Z")

</div>

That almost looks more like some code for debugging. Can you just suppress (or delete) the loop there? I don’t know any benefit of just displaying on the screen the robot counting from 0 to whatever you typed in…

---

<div class="post-metadata">

**Author:** ![MarcoD](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@MarcoD](https://forum.universal-robots.com/u/MarcoD)\
**Post date:** [August 2, 2024, 9:58am UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/9 "2024-08-02T09:58:56Z")

</div>

If I suppress or delete the loop, the program continuously ask to enter the quantity.

I have now changed the program as follows

Before Start:  
Count:=0  
OperatorInput:= “enter quantity”

Robot Program:  
Prod\_quantity:=OperatorInput  
IF count ≥ Prod\_quantity  
Popup “READY”

Somewhere in the program:  
IF sens1=TRUE AND swi2=TRUE  
Count:=Count+1

For now I want to change the “Popup” action in a “Stop” or Re-enter quantity action…

---

<div class="post-metadata">

**Author:** ![tommipocbp](https://avatars.discourse-cdn.com/v4/letter/t/ec9cab/32.png) [@tommipocbp](https://forum.universal-robots.com/u/tommipocbp)\
**Post date:** [August 2, 2024, 10:39am UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/10 "2024-08-02T10:39:35Z")

</div>

Hi,

I don’t know if this helps but this is how i managed to do it.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/c/cca58ad47116e1e9acf6923170170f8430576b86.jpeg)

---

<div class="post-metadata">

**Author:** ![eric.feldmann](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@eric.feldmann](https://forum.universal-robots.com/u/eric.feldmann)\
**Post date:** [August 5, 2024, 1:20pm UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/11 "2024-08-05T13:20:57Z")

</div>

Sorry, so are you good now? Or are you asking how to ask the operator to re-enter a value? Because all you have to do for that is copy the line in the before start, the “OperatorInput = ‘enter quantity’” line and then reassign count to 0 and you’re done.

---

<div class="post-metadata">

**Author:** ![MarcoD](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@MarcoD](https://forum.universal-robots.com/u/MarcoD)\
**Post date:** [August 5, 2024, 2:16pm UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/12 "2024-08-05T14:16:58Z")

</div>

I don’t now why this is not working for me, the program is still running after the specified quantity has been reached.

---

<div class="post-metadata">

**Author:** ![MarcoD](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@MarcoD](https://forum.universal-robots.com/u/MarcoD)\
**Post date:** [August 5, 2024, 2:22pm UTC](https://forum.universal-robots.com/t/loop-according-to-variable/17653/13 "2024-08-05T14:22:16Z")

</div>

After adding the 2 lines, it works fine by me!

Thanks guys, for helping me!
