SFML community forums

Help => Network => Topic started by: kapesu8 on July 05, 2012, 08:57:39 pm

Title: Problems with packets.
Post by: kapesu8 on July 05, 2012, 08:57:39 pm
This is a part of my code,
void GlobalWorld::SendPlayers(sf::TcpSocket * client) {
        if(this->Players!=NULL){
        sf::Packet SentPacket;
        SentPacket << this->numPlayers;
        for(int index=0;index<this->maxPlayers;index++){
                if(this->Players[index].Connected){
                        //the current player is connected, add the data to the packet
                        SentPacket << Players[index].Position.x << Players[index].Position.y << Players[index].Rotation << Players[index].Health << Players[index].Name;
                }
        }
        int x;
        float z;
        SentPacket >> x >> z;
        std::cout << x << z;
        client->send(SentPacket);
}
}

The x,z part is used for debugging so every time I call that function z will result as undefined float, however if I print the value which z should be (Players[index].Position.x, Position is vector2f) it prints the real result.
But the x matches with the numPlayers member of the GlobalWorld.
What's wrong?
Title: Re: Problems with packets.
Post by: Laurent on July 05, 2012, 10:44:33 pm
Have you checked that the following line is executed at least once?
 SentPacket << Players[index].Position.x << ...
Title: Re: Problems with packets.
Post by: kapesu8 on July 05, 2012, 11:15:53 pm
Yes if I make it print the Players[index].Position.x on that same block it'll print it correctly.
Title: Re: Problems with packets.
Post by: Laurent on July 06, 2012, 08:19:16 am
You should try to reproduce this problem in a complete and minimal code, so that we can test it.
Title: Re: Problems with packets.
Post by: kapesu8 on July 06, 2012, 11:56:17 am
Ok, I got it working.. Sorry for wasting your time :P

It was obvious mistake, the Connected member was never set to true since I modified the constructor a bit :P
Title: Re: Problems with packets.
Post by: Laurent on July 06, 2012, 12:10:24 pm
If Connected was always false, how did you succeed to "print the Players[index].Position.x on that same block correctly"?
Title: Re: Problems with packets.
Post by: kapesu8 on July 06, 2012, 12:39:53 pm
Sorry, I might have been printing it on the one outer block from that.