Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Receiving enum via sf::Packet  (Read 3264 times)

0 Members and 1 Guest are viewing this topic.

kinglime

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Github
    • Email
Receiving enum via sf::Packet
« 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.

kinglime

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Github
    • Email
Re: Receiving enum via sf::Packet
« Reply #1 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?
« Last Edit: November 28, 2013, 05:44:23 am by kinglime »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
AW: Receiving enum via sf::Packet
« Reply #2 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Receiving enum via sf::Packet
« Reply #3 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.
Laurent Gomila - SFML developer