Socket.IO handling multi-player movement in open-world

532 Views Asked by At

I am building a multi-player server for an open-world game I am building using Unity 5. I've chosen Socket.IO to be the real-time framework as I know it more than any other real-time framework.

How can I store each active player's socket in a way which would allow me to sort out each player by their distance from a cord. (for example, to only update a moving players position to players who are nearby rather than sending out to all the players on the server)?

1

There are 1 best solutions below

2
On

This sounds more like a data structure issue to me. A simple way that comes to mind would be to have:

Zones - this is a regional breakdown of space in the game
Player - this represents an individual playing
Socket - the means to communicate to a given player

The Player should have a reference to the Socket to communicate. player.SendMessage("hey there");

A zone should have a list of players

zonePlayers = new List<Player> Players();

if a message is to be broadcast through the entire zone simply iterate over the player list and send that message to each player.

foreach (Player player in Players) {
  player.SendMessage(msg);
}

Figure out how to send a message to a player. Figure out how to logically group players together.

I just read about Socket.IO and they already have Rooms. If you define a room per zone and just have a player register for that room and leave that room when they entire / leave the zone - you can utilize already built in functionality for messaging the group.