1
\$\begingroup\$

Background

I am creating a networked web game using NodeJS, Express, and SocketIO (Note, however, that this question is agnostic to implementation details). I have implemented a client-server architecture which works well when the server is run locally, but when it is hosted in a remote location there is significant lag that introduces a problem (Note that the server is still reasonably close to the client for a web game, being a few hundred miles away).

Current Implementation

The game takes place on a grid in which players occupy a single coordinate space. Players can move one space in one of the four cardinal directions "per turn". A "turn" is a single 500ms fixed loop that runs on the server.

The client works like this:

  • When a player presses a directional key, notify the server it has been pressed.
  • When a player releases a directional key, notify the server it has been released.
  • When the client receives updates from the server, render them.

The server works like this:

  • Whenever notified a player has pressed a key, store its pressed value as true.
  • Whenever notified a player has released a key, store its pressed value as false.
  • Every 500ms, process a turn:
    • For each player, if a key's pressed value is true, move the player in the corresponding direction if another player does not occupy the space.
    • Send all players the updated grid and player positions.

Issue

The problem I am noticing is that, due to the non-trivial amount of lag in network communication between server and clients, a player will have a difficult time moving one space instead of two. This is because, I assume, that by the time they have received their updated position, they are still holding down a direction key at the point in which the server is processing the next "turn". They therefore move an additional space past which they wanted to stop.

What are some common techniques I can implement to handle this scenario such that the player is easily able to predict and control their movement?

Considerations

Some things to note:

  • I considered rendering the updated position on the client before sending it to the server, but there is a problem with this approach. A player cannot occupy the same space as another player. So if we consider that a player may not be able to move into what the client assumes is a free space, there will be a glitch in showing the player that they have moved, when in fact another player has taken the space they wanted to move into during that turn.
  • It would be a poor user experience if the user were unable to hold down directional keys to keep moving.
\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.