Sending/Receiving the Game World

Started by
4 comments, last by hplus0603 11 years ago

Hi all, I've been working on a sort of side-scrolling, shoot em up, RTS game for the past few weeks and am now confronting the issue of sending the game world to the client when it logs in. Right now, when the player logs in, the entire game world is sent all at once (each object gets it's own packet) over reliable UDP (I'm using enet). Originally, I had the game world all in one packet, but the packet was too big to send in just one packet (it's like 50kb). Right now, it works fine if you have fast internet, but with slow internet (50 kilobytes per second) the client will hang there for a very long time receiving only a few game objects a second and never really receives the entire game world.

Any suggestions? I'm about to implement a system where it spreads the packets out over a few seconds, is that a good fix? Or are there fancy ways that would be better?

Current Project

World Storm

out our Android app! (My roommate and I)

https://play.google.com/store/apps/details?id=com.drsupersocks.ninja

Advertisement
How are you sending the data? And how do you go about receiving it? Transmitting 50KB of data even over a slow connection (barring dialup) should take less than a second.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

You don't need to send the whole world, just the area around the players location. Generally, an RTS game has both players starting together so there's no need to send any data initially. If you have a hot joinable game, then sending only the players immediate surroundings will help improve bandwidth. You should generally be sending delta's after the clients are synchronised. Perhaps expand on your requirements if that doesn't help?

But, as Apoch says, 50k should transfer pretty quickly.

n!

If the problem is that Enet limits the number of packets per second (with a small send window, say) then you should cram as much as you can into each packet. Let's say Enet allows up to 1300 bytes per packet, then you should cram as many entity updates as you can into that one packet. 39 packets later, you will have sent everything.<br />
enum Bool { True, False, FileNotFound };

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 biggrin.png 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.

Current Project

World Storm

out our Android app! (My roommate and I)

https://play.google.com/store/apps/details?id=com.drsupersocks.ninja

A good ball park number for maximum payload size is 1300 bytes. This works well with the vast majority of networks out there, and is big enough that the per-packet overhead won't be more than a few percent.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement