• Advertisement
Sign in to follow this  

What exactly do I send in packages?

This topic is 982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So question is basically in the topic title.

 

In a pong game I would send e.g the following:

  • Player1/2 x-/y-Pos
  • Ball Pos

 

Luckily SFML already knows that these values in the packages are float numbers e.g. .

 

Question is: If it would be a byte stream only, how shall the receiver know that it is a float number? Putting some kind of identifier in it as a prefix?

 

And let's say there are 6 Players and 3 NPCs, every player is able to collect items. So I have to send the positions every single tick and in case someone collects an item than this information too. 

 

But how do I encode it into a datastream? Like player2 collected weapon1. What Data would you send to your server? Some kind of code?

 

 

Like:

std::string message = "Something to tell that player2 collected weapon1";

sf::Packet packet;
packet << message;
socket.send(packet, recipient, port);

Edited by Argha

Share this post


Link to post
Share on other sites
Advertisement
Hi. It all depends on the protocol you want to use.
Like http you build a sting with command tokens.

Or you can roll your own protocol
Using something like google protocol buffers.
That will create data classes that can be serialized to a device independent format that can be transmitted over the wire.
With the proto buffer you can put in a message type identifier uneque for each message then when you receive a whole message you check the type and parse the data as that type.

Like
Class protoMsglocation
{
Public:
Int32_t Type;// the message type for this message only
// other data members go her
}

Then in some header you define all your message types

Share this post


Link to post
Share on other sites
For a real-time game, HTTP POST is about as bad a match as you can find.
The overhead per request is staggering, and the latency is really high.

Share this post


Link to post
Share on other sites

For a real-time game, HTTP POST is about as bad a match as you can find.
The overhead per request is staggering, and the latency is really high.

I just meant that I would/could encode the data via UDP package that way, like sending a string "playerId=01&posX=-333&posY=47"

Share this post


Link to post
Share on other sites

I would/could encode the data via UDP package that way, like sending a string "playerId=01&posX=-333&posY=47"


Or you could do what most games do, and define a binary protocol. (See the FAQ)

Specifically, if there can be 255 players connected to an instance, and positions have a range smaller than -32767..32767 for X and Y, you can pack what you need into five bytes:

playerid: 1 byte
xpos: 2 bytes
ypos: 2 bytes
Each kind of message you may want to send is typically known as a "PDU" (protocol data unit or packet data unit) and you typically put a PDU identifier in front of the data so the receiver can know what they're receiving. You'd also use some kind of framing that takes care of things like loss detection, latency measurement, authentication, etc.

Share this post


Link to post
Share on other sites

Yes, I read about that too. But I also think that my little game won't be that critical performance wise :)

Anyways, thanks, then I might go that way

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement