2D Game commands delay

Hi!

I’m working on a 2D game.
There are two players (in multiplayer) and about 350 entities (mobs and other).
To synchronize mobs i used a random with the same seed on both devices.
But to move the player and to fire, i send the commands to the other player through TCP.
It works fine but sometimes there are delays.

Is there a better way to manage players movement?

Thanks!!!

Socket.setTCPNoDelay(true);

Thank you but i already use that!!

Then we need to see code.

I have a class “Networking” with two threads:

  • the first one sends packets in outgoing list;
  • the second receive packets and i have a “handle” method to manage each package.

Handle method:


if(packet instanceof PlayerMovePacket) {
	if(isServer) players[1].incomingCommands.add(new PlayerMoveCommand(((PlayerMovePacket) packet).getDir()));
	else players[0].incomingCommands.add(new PlayerMoveCommand(((PlayerMovePacket) packet).getDir()));
}
else if(packet instanceof PlayerFirePacket) {
	if(isServer) players[1].incomingCommands.add(new PlayerFireCommand(((PlayerFirePacket) packet).getFire()));
	else players[0].incomingCommands.add(new PlayerFireCommand(((PlayerFirePacket) packet).getFire()));
}

Then, in the Player class i process all commands in “incomingCommands” list and for each command i move the player in that direction.
Do you need anything else?

Thank you!

Hard to tell from just that. Try enabling -verbose:gc and seeing if the pauses coincide with a gc cycle.

But is the reasoning right?

Is it the way I would organize things? Not really. But it does work I suppose. There’s all kinds of reasons things could hitch up, from gc thrashing to oversynchronization to network lag, but a half dozen LOC doesn’t really say much. I can’t tell how expensive your command constructors are, or what collection implementation you’re using, and so on.

Performance troubleshooting is tricky enough, intermittent performance problems are maddening. If you can find out what changes in the program or the environment cause the intermittent problem to get worse or more frequent, you’re more than halfway to figuring out the cause.