Before I start with the two client progress I must mention that I added the ability to provide a username at the start menu and made that username follow the train around so that we can see whos who on track (eventually).
Today I made a major breakthrough that should have been a minor one. Allow me to explain. During the flow of a game there are only 3 messages that a client knows it’s going to recieve, all three come from the server directly telling the client how to initialize the track. Because we know these are coming we can sit and wait for them. During client setup my code waits a set amount of time for each of these messages and if the amount of time is used up or a message is corrupt, they disconnect. This wait for the message method only works when we know a message should be on its way because waiting blocks all other code from executing. The way around this is to run an asyncListen() instead of the regular listen function. Asynchronous listening creates a second thread which blocks until a message is received. This means that I can listen all the time in the background while simulating the game at the same time. This function receives all of the other messages like when another train joins, a turnout is clicked, a train changes speed, ect. asyncListen() terminates just like a regular listen when it receives a message. In order to always be listening I simply had a bool listening and a function StartListening(). Every tick I check the listening variable and if the listening variable is set to false I call start listen which starts the asyncListen() and sets listening to true. What I forgot to do was set listening to false when the asyncListen() returned. So what was happening was the variable started as false then a listen started and it turned to true then never turned back to false so only the first message was received. After fixing this I now have some things working from client to server to client. For example, I can change a turnout on one and it changes on the other but a disconnect does not remove the train from the track for other clients.
Leave a Reply