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

Author Topic: Sending Different Types of Data [SOLVED]  (Read 2096 times)

0 Members and 1 Guest are viewing this topic.

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Sending Different Types of Data [SOLVED]
« on: March 05, 2014, 05:21:41 am »
I am making a game and have it working quite well, except I haven't implemented multiplayer. I read all the networking tutorials, and I have made and successfully tested a few network programs.
One thing i'm not sure about is how to go about solving this problem:

If I want to send different types of messages, like one to update the position of an object, and another message to create a new object at (coords), how do I tell the receiver to correctly interpret the message as one or the other, because different scenarios require different types of data.

The solution I thought of was to have a sf::Packet, with a char at the front(I chose char because it's small), and depending on the value of the char, the receiver would attempt to extract the data that goes along with that indicated type, psuedo code being:

Recieve packet;
packet >> char;

if(char == 0)
packet >> int;

if(char == 1)
packet >> other types of data;

if(char == 2)
packet >> more data to do other things;
Also, can I get any smaller than a char?
« Last Edit: March 05, 2014, 05:53:49 pm by Strikerklm96 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sending Different Types of Data
« Reply #1 on: March 05, 2014, 07:44:03 am »
You have found a solution that works well, seems rather logical and efficient, so what exactly is your problem? :P

Quote
Also, can I get any smaller than a char?
No.
But if one day you add other "header" information (such as a protocol version or a CRC) which is smaller than 8 bits, you can possibly stuff it into the same char.
Laurent Gomila - SFML developer

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Sending Different Types of Data
« Reply #2 on: March 05, 2014, 05:52:45 pm »
You have found a solution that works well, seems rather logical and efficient, so what exactly is your problem? :P

The problem is that most of my other solutions to problems using libraries have been ones that don't work well, and are not logical or efficient, and have already been solved :) so just checking to see if there was a better solution. Thanks!