Unity3D-Python-Communication
Unity3D-Python-Communication copied to clipboard
Sending message based on button press
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.
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
}
}
this is such a lifesaver
I try to use a queue, but every time I click "played", unity freezes :(
@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.