Search code examples
javamultiplayer

Correct way of sending data of a player in java


I have some knowledge in using nitro-net for developing my games. As of now, I am sending data of the player like this:

  • Player attacks
  • Client sends data to server that player has attacked
  • Server sends this data to every other connected client
  • The clients that receive the data check what action was sent ex ("attack_left")
  • An if statement checks what action was sent, then the client has the player execute the code for this action

Is this the best way of handling sending data to a server and then to multiple clients? Should I just send a full player object instead?

Thanks!


Solution

  • In general, yes. It is best to only send the data you need. Sending the entire character's information can quickly become an issue if there are a lot of other players connected to the server.

    For example, if you have 10 players all attacking at the same time, then the server receives 10 attacking commands from each client. The server then will have to send each of those attacking commands to each other client that is connected. So you end up with 90 [10 x 9 = 90] commands that are sent out by the server. If each of these commands is sending the entire character's information, that can easily get out of hand.

    If only a limited number of players are connected at a time though, you might be able to get away with sending the entire character's information, but you should decide how many players you want to support for your game.