0

I am working on a multiplayer game. Each client have a character that moves in a shared environment.
I use socket.io to create rooms and peer.js to create a peer-to-peer connection between clients.
What I am trying to do is to enable each client to update the positions of the characters of the other players in his map.
For that, each client should have the state of the keyboard cursors (arrow keys) of the other players so that he can move their corresponding characters with a walking animation.

P2P: I am thinking of creating duplex streams between the clients so that each client will have the state of the keyboard cursors of the other players, so that he can move their characters with the appropriate animation...
SOCKETS: I can also pass the information via the server using sockets, but i will have to send an update of the cursors state 60 times per second since the game is on 60 fps, which makes a lot of socket messages. I am not sure this is the most efficient way to handle it

What is the most efficient way to keep everybody updated about the state of the other players ? Any suggestion will be appreciated. Thanks.

1 Answers1

1

Actually your game is likely a little demo of a MMORPG game or something like CS/CSGO.

For such a game, we always have a loop in main process (work process) with a frequency like several frames a second(say 20 frame). In every frame, the client will process the packets received from others and the options form the pleyer.

If here is 20 frames, that means every fram can't be more than 50ms, so it will cause some delay if it's in WLAN and some packets drop happend.

If you want to use P2P to synchronized players action, here is a problem: when player number booms, the comlicated of the connection boos too. What's more, you need a reliable connection protocol, that means you need to know how to use something like QUIC or write a reliable UDP by yourself.

So I think the most effcient way may keep use C/S model insetead of P2P only if your game will use in LAN and there is quite a few players.

tyChen
  • 1,404
  • 8
  • 27
  • I see. And for the packets shared between players, is it a common practice to share the cursors state or to share the position of the characters in the map ? If i just share the position, i don't see how i can reproduce the moving animations smoothly.. – Achraf El Khamsi Feb 14 '21 at 09:26
  • That is why C/S is better, it broadcast the player's new position around you in some specific area every frame instead of broadcast to all every time some move by p2p. – tyChen Feb 14 '21 at 09:49
  • I mean i can also use C/S to broadcast the move (or the absence of move) of the player at each frame...but for precison i think it's better to broadcast the position. I will try to work with a C/S model.. – Achraf El Khamsi Feb 14 '21 at 10:02
  • You need to know that the trully precision is not needed cause our human can not feel something like that, and the frame is a trade-off for balance. – tyChen Feb 14 '21 at 16:46
  • I worked with the C/S model. Each player sends his coordinates at every frame to the server, which broadcast the information to the players in the same room. It worked perfectly! – Achraf El Khamsi Feb 16 '21 at 01:01
  • That's good! And when your game became more and more complicated, you will need to detach the central loop, but it's a little far for you at recent. – tyChen Feb 16 '21 at 02:38
  • Do you have some good resources where i can read more about handling multiplayer, especially for a complicated game with a lot of users...because even if it works fine right now, i could see that it would be problematic if the number of players explodes.. – Achraf El Khamsi Feb 19 '21 at 00:51
  • If the game is for sale/earning money, the best way for that is to buy better devices, it's relatively cheap and easy. If you want to optimize your game's program, then when players' number booming, you may need distributed system or divide players to different zones like WOW. – tyChen Feb 19 '21 at 01:26