Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
  1. #21
    Originally Posted by JerryMotov View Post
    Did you also try moving the players simultaneously. It mostly occurs when more players are trying to move.
    Ah, that makes sense now. agoose was right about the issue being the client not emptying the receive buffer every frame. The issue only comes up when more than one client is sending data since the send and receive rates are equal in that case (1 position update per frame). When two clients are moving, you receive 2 position update per frame, but only process one. You need to put all the code in the client's receive method into a loop that breaks out when you get a socket error when there's nothing to receive. You can use a while loop, but I recommend using a for loop with a reasonable range (maybe double the maximum amount of packets you expect to receive per frame) to ensure the client won't completely freeze if it ever starts receiving packets faster than it can process them.)



  2. #22
    Originally Posted by Mobious View Post
    Ah, that makes sense now. agoose was right about the issue being the client not emptying the receive buffer every frame. The issue only comes up when more than one client is sending data since the send and receive rates are equal in that case (1 position update per frame). When two clients are moving, you receive 2 position update per frame, but only process one. You need to put all the code in the client's receive method into a loop that breaks out when you get a socket error when there's nothing to receive. You can use a while loop, but I recommend using a for loop with a reasonable range (maybe double the maximum amount of packets you expect to receive per frame) to ensure the client won't completely freeze if it ever starts receiving packets faster than it can process them.)
    Now you say it it does sound logical that this is the problem.
    I tried what you said and it does stop the "slow-motion effect", but it causes a laggier effect

    What I tried

    Changing the receive method to this:
    Code:
    def recieve(self):    try:
            for i in range(8):
                # Listen for incoming packets
                data, addr = self.server.recvfrom(1024)
                self.queue.append(json.loads(data.decode()))
        except socket.error:
            pass
    and creating a new function which handles the packets in the queue
    Code:
    def handle_packets(self):
        for packet in self.queue:
            if 'new-connection' in packet.keys():
                -- Code that handles the packets in the queue--
    The result


    I feel like we are really close to solving this now but I am not sure what I can do against the laggy effect.
    Last edited by JerryMotov; 19-Apr-18 at 03:26.



  3. #23
    Originally Posted by JerryMotov View Post
    Code:
    def recieve(self):
        try:
            for i in range(8):
                # Listen for incoming packets
                data, addr = self.server.recvfrom(1024)
                self.queue.append(json.loads(data.decode()))
        except socket.error:
            pass
    You forgot the break condition, and the for loop needs to be outside of the try-except clause. Try this.
    Code:
    def recieve(self):
        for i in range(8):
            try:
                # Listen for incoming packets
                data, addr = self.server.recvfrom(1024)
                self.queue.append(json.loads(data.decode()))
            except socket.error:
                break
    Also, I can't tell if you're doing this or not in your new code, but make sure you're emptying out the queue every time you go through all the packets in it. Otherwise the game will continually slow down as you process old data that excessively builds up in the queue.

    If you still can't get things working, go back to your original code and just put all the code in the receive method inside the loop (and include the break statement after the except socket.error). You can also try greatly increasing the loop range as it shouldn't hurt anything as long as you break properly. That's what I tested, and it fixed the issue without any side effects.



Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •