# Sending entities to connecting player

When a player connects to a game, he needs to be sent all the entities currently on the map. This is done by creating entities one by one. Three ways of implementing this come into my mind. Which one them is the best?

1) Implement sending entities in the game engine: Game engine listens to "connected" event from a player object and send all the entities to the player when this happens.

2) Implement sending entities in the entity class: Entity listens to "player connected" event from the game engine and when this happens, it sends itself to the connecting player.

3) Implement sending entities in the player class: When a player connects, it gets list of entities from the game engine and sends each of them to itself.

• There isn't a best method. Pick the one that makes the most sense for your architecture and requirements. If you make a list of some details like if you're using client-server, or peer-to-peer, the bandwidth requirements for sending entities, the frequency of players joining/leaving, etc., you can create a little pros/cons list of your own for each method and pick the one that best fits. Jul 17 '13 at 17:43

1) Implement sending entities in the game engine: Game engine listens to "connected" event from a player object and send all the entities to the player when this happens.

While none can say what is the best, this is indeed a good way to do it. Server listens for connections and when it receives new connection, it sends "The Data" packet, that contains all the necessary info, that client needs to be able to start rendering the world.

Semi-Pseudocode to demonstrate Option 1:

while((msg = netStream.Receive()) != null)
{
if(msg.Type == PacketTypes.NewConnection)
SendStartPacketTo(msg.Sender);
}


For me ( my opinion ), this approach gives better control, on when to send and what to send.

If you are using component based programming, like in Unity3D, then you might want entities to send data independently. ( Option 2 ).

• Thank you for your answer. I decided that best solution would be the one that requires the fewest lines of code. Option 2 only required one, specifically: sigConns.push_back(ge.onJoin.connect(bind(&Entity::sendCreate, this, _1))); Jul 18 '13 at 14:36