SFML community forums

Help => Network => Topic started by: kinglime on November 28, 2013, 05:37:08 am

Title: Receiving enum via sf::Packet
Post by: kinglime on November 28, 2013, 05:37:08 am
I have my packets set up so that the first thing I send in them is a header. My headers are stored in an enum, like so:
Code: [Select]
enum networkHeaders {PlayerMove, ClientDisconnect, Test};
I can send the packet just fine, like so:
Code: [Select]
outgoingPacket << header
But I cannot receive the packet, like so:
Code: [Select]
incomingPacket >> header
Also this is my header variable:
Code: [Select]
networkHeaders header;
I would appreciate some help here, I tried searching the forums. Enums and networking are new concepts to me but I'm eager to learn. Thanks in advance.
Title: Re: Receiving enum via sf::Packet
Post by: kinglime on November 28, 2013, 05:40:28 am
Seconds later I believe I have found the solution to my mistake  :)

I did the following:
Code: [Select]
int x;
incomingPacket >> x;
networkHeaders header;
header = (networkHeaders) x;

That seems to do the trick. I don't know if that is the best way to do this, if there is a better way please let me know. Thanks!

Edit: Is there some way to modify sf::Packet& operator >> to do this automatically?
Title: AW: Receiving enum via sf::Packet
Post by: eXpl0it3r on November 28, 2013, 08:06:18 am
You said "it does not work", but how exactly do you notcie that?

You shoulx use C++ casts, like static_cast<type>.
The issue here is, that C++ doesn't allow the implicit conversion from int to enum and it's nothing SFML cablln and should do against it. ;)
Title: Re: Receiving enum via sf::Packet
Post by: Laurent on November 28, 2013, 08:14:27 am
networkHeaders is a custom type defined by you, SFML knows nothing about it. So if you want operators << and >> that handle it correctly, you have to write them. See the tutorial for more details.