Yes, it's a hot joinable game. Currently, each object get's it's own packet in the initial world send, and I mass send all of them at once.
Here is the relevant code:
void NetworkManager::send(sf::Packet &packet, int connectorID, int excludeID, bool reliable)
{
sf::Packet finalPacket;
//finalPacket << sf::Int8(0);
finalPacket.append(packet.getData(), packet.getDataSize());
// Create the enet packet
unsigned int flags = 0;
if (reliable)
flags |= ENET_PACKET_FLAG_RELIABLE;
ENetPacket *enetPacket = enet_packet_create(finalPacket.getData(), finalPacket.getDataSize(), flags);
if (mType == NetworkType::CLIENT) // Clients send data to server only
{
enet_host_broadcast(mHost, 0, enetPacket);
}
else if (connectorID > 0) // It's a server and the client is specified. Tell only that client!
{
enet_peer_send(findConnector(connectorID)->mPeer, 0, enetPacket);
}
else // It's a server and the client is unspecified. Broadcast to everyone
{
if (excludeID > 0)
{
for (unsigned int i = 0; i < mConnectors.size(); i++)
{
if (mConnectors->mID != excludeID && mConnectors->mPeer)
enet_peer_send(mConnectors->mPeer, 0, enetPacket);
}
}
else
enet_host_broadcast(mHost, 0, enetPacket);
}
}
void NetworkManager::sendSceneCreation(int connectorID, int excludeID, bool reliable)
{
for (unsigned int i = 0; i < SceneManager::get()->getCurrentScene()->getGameObjects().size(); i++)
{
if (SceneManager::get()->getCurrentScene()->getGameObjects()->getSyncNetwork())
sendGameObject(SceneManager::get()->getCurrentScene()->getGameObjects(), connectorID, excludeID, reliable);
}
}
void NetworkManager::sendGameObject(GameObject *object, int connectorID, int excludeID, bool reliable)
{
sf::Packet packet;
packet << PacketType::CREATE_OBJECT;
object->serialize(packet);
send(packet, connectorID, excludeID, reliable);
}
Thanks for the suggestions, I'll cram stuff into bigger packets. Could you recommend a good maximum packet size? I'll make it adjustable and play with it, but I'd like a ball park This networking stuff is all very new to me.
And thanks, nfactorial, I've also given sending only immediate surroundings a thought, and that will be very easy since I already have stuff to detect which planet you are currently on (you fight across many 2D planets).
Edit: But also, in that regard, sending only the current surroundings is kind of the scenario at hand, since I only have 2 planets in the game right now. I plan on having around 100 planets in an actual match.