If-Statement in URscript not working

Greetings URCommunity,
I am trying to write an if-statement in an URscript but I haven’t been successful.
Before the if-statement, I have this while loop:

\r\nwhile(get_standard_digital_in(0) == False):
sleep(0.01)
end
This loop works perfectly. I thought the same method would apply to the if-statement but after many tries, I am not able to run the code. This is what I have now:
\r\nif(get_standard_digital_in(0) == True):
var_1 = True
end
Before the while loop I have:
\r\nglobal var_1 = False
Unfortunately, this code does not run with the if-statement, even though it runs fine without it so the trouble is definitely in how I am writing it. Any help is much appreciated.
Best regards,
Pedro Monforte

pmonforte77,

Do you get an error when you try to run it?
if so what error

are you trying to put the If inside the while loop?

remember you need an end for each statement
def name()
while

if
end
end
end

Hi Hammerton, I did not get an error. Usually, when the script is not fit, it doesn’t even allow it to run, like this case. So, in reality, I sent it via tcp/ip and the program was not even recognized. I have, however, found a solution for this. For some reason, whose cause I know not, the program does not run if on the if-statement there is only a line assigning a variable but if I add sleep(0.01) after it, it works perfectly.
Thank you anyway

can’t you just program an if statement in polyscope and view it as script to see what you are doing wrong?

program an if statement and save the program. then open a new program and input a script command but choose “file” instead of line in the top right. then open up your if statement program and you should be able to see it in script.

1 Like

Have you figured out the cause? I ran into the same problem with the IF-statement, I was doing something like
payload = socket_read_string(SOCKET_NAME)
if payload=="something":
do something
end

The string that was sent over the socket is something, but it does not go into the condition and execute my commands. @pmonforte77

Not saying this is your exact issue, but when programming URCAPs in Java, it also doesn’t evaluate == correctly for strings. You have to use object.equals(“something”) to test for equivalency. I’m not sure what the URScript analog would be there.

@eric.feldmann Thank you for pitching the idea. Yeah, I figured it out last week. Apparently, the string is either not transferred using the same encoding from the remote PC, or == only compares the object id/instance instead of the value.