Universal Robots Forum

Loop according to variable


This is probably very simple thing but i 'm not very familiar with script yet :slight_smile:
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.


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

OperatorInput:=‘Number of times to loop’

Then you can add the loop

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!

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 @michael.r.mercer points out, you can use the Assignment node and switch it to Operator as the source

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

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

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.


(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.

1 Like


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 :slight_smile:

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