Unity3D-Python-Communication icon indicating copy to clipboard operation
Unity3D-Python-Communication copied to clipboard

Sending message based on button press

Open jBachalo opened this issue 4 years ago • 4 comments

First of all, thanks for sharing this with the community. Having a problem simply sending a message after the initial handshake, say from some UI input like a button press. Most likely because of my lack of understanding on how threads work in Unity.

Any chance you might post a quick example? If not a button then a simple message sent from HelloClient a few seconds after launch?

Thanks in advance.

jBachalo avatar May 27 '20 22:05 jBachalo

If you want that when a button is pressed, it should do something, then you can define a request queue as a field of the HelloRequester class. And whenever a button is pressed, you add a request message to the queue. In this case, the request message could be random because you just want to request without parameters. Just replace the for loop with

while (true) {
  if (there is a request in the queue) {
    request = queue.Dequeue();
    // send the request to server
    // wait for the response
    // use the response to do whatever you want the button to do
  }
}

offchan42 avatar May 28 '20 15:05 offchan42

this is such a lifesaver

rileyshu53 avatar May 02 '22 16:05 rileyshu53

I try to use a queue, but every time I click "played", unity freezes :(

rileyshu53 avatar May 03 '22 02:05 rileyshu53

@rileyshu53 It's probably because you are blocking the main thread somehow e.g. by an infinite loop or handling messages in the main thread. Make sure you aren't doing that. There are the main thread (Unity Update() function) and the hello requester thread. The requester thread can be blocked but the Unity's main thread must never be blocked.

offchan42 avatar May 03 '22 03:05 offchan42