How to change a variable value from scriptWriter

Hello,

First of all I want to say that I’m new creating URCaps, so if you see that it exist another easier way to create this functionality I will hear your suggestions gratefully.

I have created a button in my urcap that creates a log entry with the texts that I have in two text fields after I push the button.

image

I have created this functionality following these steps:

  • I have a button that executes selectCreateTicket function when the button is selected:

  • The function selectCreateTicket only sets to “True” the variable CREATE_TICKET_STATE_KEY:
    image

  • I have another function named getCreateTicketState that returns the value of the variable CREATE_TICKET_STATE_KEY:
    image

  • In the generateScript function I have an IF statement that checks if function getCreateTicketState returns “True” and in that case it creates a log entry using textmsg function:

The problem is that this code after I push the button it starts creating the log entry constantly, but I want to create only one time per button push. So if you can see that I have commented a line that executes the function deactivateCreateTicketState in the IF statement of the generateScript function.

  • This is the code of the deactivateCreateTicketState function (it writes “False” in CREATE_TICKET_STATE_KEY):
    image

If I uncomment this line it gives me this error: The method appendLine(String) in the type scriptWriter is not applicable for the arguments (void).

Do you know how can I get this functionality?

Thank you!

To address the error you’re seeing, this is because you’re missing a quote. However, even if you put the other quote in, you would then be attempting to call the function deactivateCreateTicketState() from inside URScript (because it’s all inside a string). At this point your script generation will likely fail with an error similar to “function deactivateCreateTicketState() not defined.” It’s easy to forget/mix up functions that exist in java, and those that exist in URScript, so try to keep them straight.

As for why it’s executing constantly, that’s probably due to the program being set to loop infinitely, and this code existing in a URCapProgramNode. By contrast, you can also create Installation Nodes whose script is executed only once per press of the start button/save button. You could also try placing this node in the Before Start sequence (tap the main Robot Program and tap the checkbox saying “Add Before Start Sequence”) as I believe this code is also only executed once.

However, because you asked to be informed of a better way of doing this, I’ll leave you with this link:

It is a class that contains many useful methods that allow you to run snippets of URScript directly from Java. So essentially, you would put in your action listener something like

URScriptCommand.sendCommand("textmsg(blah blah blah)");

And this script would be executed right away, each time the button is pressed, without needing to actually run your program. If this sounds like something you’d be interested in, I can try to go into more detail, otherwise you can do some further research into this class file. But just know that it is possible to execute single lines of script (or any number of lines) as a direct result of interacting with UI components on the Java side.

Hello!

Thank you for your help! Now I can create a log entry after I push the button using Script sender sendScriptCommand.

In my robot (now I am using URSim) I am using two urcaps: my personal urcap and ellipseswing urcap to test articulation movements. When I play the program continuously is running the ellipse code, because my personal urcap doesn’t do nothing until I push the button or the text field. The problem that I have now is that when I push the button (it works in the same way with the text field) all the program stops. I want to know if it’s possible to push buttons and the other ellipse program can continue working.

Thank you!

This is possible. Within that class, there should be a function to “setAsPrimary()” or “setAsSecondary()” or something similar. When you set a script command as Primary, it will consume real time. This will stop your program, as you can’t continue moving the robot (consuming real time) and then tell it you’re going to do something else that consumes real time.

So, in your case, displaying a text message is not something that needs to consume any real time, so you should be able to use the setAsSecondary() command. The robot will execute this function in the background without stopping the currently executing code.

Thank you! With setAsSecondaryProgram() I have achieved the functionality that I wanted.