Call custom java function in generateScript() URCap

I have a java function that I use to launch multi object detection with a Ploc2D camera, I created a custom code called socketCommunication() that opens a socket connection with the camera’s IP and sends the detection commands, but all of it is done by using java socket library and not the socket_open() URScript method, because of some data treating that my code does. Unfortunatly, It has 2 problems, my socketCommunication() method is called only once when I press the play button in Polyscope, even when I set “Program Loops Forever” and when I tested another program putting:
1 - URCap
2 - MoveJ
3 - URCap
(This is an example of a test code)
The second URCap is called before the MoveJ is finished.

public void generateScript(ScriptWriter writer) {
	System.out.println("Begin"); // Used only for debug
	socketCommunication(writer, 14158);

My question is: Is there a way to call custom functions in the generateScript() method?

The Java Method generateScript() is only run once when pressing play or pressing save. So yes, you can call your own custom functions, just be aware they are only called ONCE from Java. It sounds like what you actually want is to call Java Functions from a URScript command. In this way, the generateScript method would write the URScript function into the actual executing script. This would allow that method to be hit multiple times, for example when the program loops forever.

For this I would look into the RPC_factory command in URScript. You can create a link between UR Script and your Java functions in this way.

Hello, thanks for the response, it’s more clear for me now, but I don’t know exactly how to implement this solution.
My socketCommunication() method is in the programNodeContribution file along side with the generateScript() method.
I looked at examples of rpc_factory use, but it’s not so clear to me how to use it.

Thansks again

Take a look at the source code in this Github repo. It is a bit more involved than my first post implied. I remember following along with this, not fully understanding what was going on, but was still able to get it implemented for my CAP.