Search code examples
c#udpmultiplayerlag

Multiplayer game in UDP "Chokes" internet


I tried to make a PONG game which works , and then change it to a multiplayer game.

You can choose the "Game Speed" which is basically how much cycles it takes until the ball can move and send location data.

So every tick (default is 200) the server sends this to

pos:{0}:{1}&ballpos:{2}:{3}&gametick:{4}

(of course its in a String.Format)

and every tick the client sends this to the server :

pos:{0}:{1}

in order to sync the clients location with the hosting player.

it sends the messages with this function :

public static void sendMessage( String message , UdpClient client)
{
Byte[] toSend = new byte[55];
toSend = Encoding.ASCII.GetBytes(message);
client.Send(toSend, toSend.Length);
}

Now when we play , and we are on skype for example , the skype connection quality is really low , and extremely laggy. then the game becomes laggy.

Is there a more efficient way to send data to sync locations?


Solution

  • Here are a few tips:

    1. Send packets based on time, not loop cycles. Loop cycles are heavily influenced by what else is going on on the PC. This gives you more control over how often the updates will be made and it will not vary based on system performance. 200 microseconds is crazy fast. You don't need that.
    2. You are flooding your local network with packets and causing a bottleneck.
    3. Send packets less frequently. 5 times per second is a good number to start with
    4. If that update rate is not quick enough, do what the professionals do. Interpolate where the ball is going to go based upon past updates. For example, you have an idea of direction and speed, so make an educated guess!

    Updates should be regular, deterministic (you know when they will happen) and non-volatile (missing one isnt a problem). Experiment with slowing the update rate right down and see what happens.