- Home /
StreamReader & Unity still does weird things
yesterday i posted a question on how i could use StreamReader WITHOUT letting unity crash.... i taken out the loop BUT theres still some weird stuff
when i start the server (a java server) en connect to it in unity, the game not freezes but it WAITS for a newline (because i use ReadLine()), wether i use ReadLine(); or Read(); it doesn't matter, it always freezes.
i just discoverd that when i send something to unity using the server, unity rendered 1 frame then freezes again. unity waits until it receives data before moving on with the other things. i tried the things told i must do to make it work but, either i didn't do what was said, or it didn't work.
i hope to finally hear a solution to this, because im already searched for this 3 days in a row now and i can't move on before i found an awnser
Script:
void Read() {
NetworkStream STREAM = connection.GetStream();
string line = null;
StreamReader IN = new StreamReader(STREAM);
String returndata = IN.ReadLine();
string[] returndata2 = returndata.Split(" "[0]);
string code = returndata2[0];
string text = returndata.Replace(code, "");
if(code == "#ChatMessage") {
BroadcastMessage("AddChatMessage", text);
}
}
void Update () {
Read();
}
It looks to me like you've got a design problem there. You are calling a Synchronous function many times per second. It just isn't the right way to do this stuff. If you want that code to work you will have to either:
Use Asynchronous IO and handle the resulting data
Use that code on a second thread and send messages to the main Unity thread when information arrives
You could also possibly use my code from this post to start the second thread and send information back to the main thread.
Ive got this almost working, but do you have any idea on how to send the messages to the main Unity thread??? Broadcast$$anonymous$$essage doesn't work because i can't call unity function inside a non-main thread
You have to set some non-Unity-API-variables which Unity reacts to next time it renders a frame. This looks like you're implementing a chat window, so I imagine you want the chatmessage to be added to a list of messages in a little scroll window?
Just add the chatmessage to a List (remember to synchronize properly using the lock keyword). Then, in OnGUI somewhere else, make a scrollview that loops through all strings in that List and draws a Label with each message.
Answer by Chesley · Jun 25, 2012 at 09:50 AM
i already solved the problem a few days ago, but forgot to write a message for the right solution. basicly Christian H Pedersen was right!
Christian H Pedersen: You have to set some non-Unity-API-variables which Unity reacts to next time it renders a frame.
Your answer
Follow this Question
Related Questions
other players and the server does not receive networkView.RPC 1 Answer
A node in a childnode? 1 Answer
How to upload a local file via the web player? 1 Answer
Unity networking tutorial? 6 Answers
How to make a game updater/launcher? 1 Answer