I am trying to get networking working for my game, I've read several sources of information about this subject but one thing is still unclear for me.

The way I process client input is by executing a command bound to that input, for example:

Player presses D -> MoveLeftCommand gets executed which applies a linear impulse to the body of the player.

Since the server codebase and client codebase are almost the same I just reference the commands by bytes, MoveLeftCommand is for example byte 1.

What I have now is when the player presses D it sends the byte of the MoveLeftCommand to the server and the server executes this on the specified entity. I don't believe this is right though since the client will have to send 60 packets per second to the server just to move. It is no problem just send a JumpCommand, this command will get executed once. But I think there is another way to send position.

My question is: what is the common way to send input from a client to a server? I have read about 'sanity checks' on servers but I don't now what is meant by that. Should I do something with client time?

Thanks in advance!

  • \$\begingroup\$ Can you clarify type of your game? \$\endgroup\$ – Kostya Regent Dec 15 '14 at 22:02
  • \$\begingroup\$ It's a 2D platformer. \$\endgroup\$ – Ives Dec 16 '14 at 8:48

Instead of sending a packet whenever the key is held down, send a packet whenever the key state changes from pressed to released and vice versa. To account for network delays you can implement some kind of extrapolation on the server side and client side utilizing the time the packet was sent and received. For this of course you have to send the time the packet was sent in the packet data.

  • 2
    \$\begingroup\$ As a note, you don't have to send the absolute time. Both sides of the connection should be continuously measuring the current RTT of the connection. The outgoing packet can record the delta between when a message was generated and when the packet was actually sent. You can determine when a message was generated in the recipient's time by taking the ReceivedTime - RTT/2 - SendTimeDelta. There's no need to record time of packet sent, only when the message in the packet was generated, and only if there's considerable delay between the two (otherwise SendTimeDelta approaches 0). \$\endgroup\$ – Sean Middleditch Dec 16 '14 at 8:40
  • \$\begingroup\$ Hmm, that's seems like the right way to do it. What do you mean by interpolating though? Do you mean that the server should go back in time to execute the command? \$\endgroup\$ – Ives Dec 16 '14 at 8:49
  • \$\begingroup\$ @Ives: my mistake, I meant extrapolation. So f.e. the server can predict where the player is at server-time based on his last velocity. \$\endgroup\$ – d3dave Dec 16 '14 at 8:53
  • \$\begingroup\$ @d3dave would be a bad idea to evaluate input client side and send to server packets containing effects data (e.g. new_position = new Vector2(x,y))? \$\endgroup\$ – Leggy7 Dec 16 '14 at 9:03
  • 1
    \$\begingroup\$ @Leggy7: that would be a bad idea because it is easier to exploit, and even if all exploited cases are taken care of, it introduces needless additional overhead on the server. Also, it makes the game less extensible because then servers cant bind special actions, only clients can, whereas with the other approach clients dont need additional mods for special server capabilities. \$\endgroup\$ – d3dave Dec 16 '14 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.