Sign in to follow this  
DarkZlayer

Tagging a packet with what 'to-do'

Recommended Posts

DarkZlayer    124
Recently I started to fool around with online programming for games (or heck anything :P) and I'm just curious if I am going about packets the right way. So the example I will use will be an online game of pong. I can get it to work but it seems like there is a better way (I am too much of a perfectionist!) so I'd like some input. So let's say the up arrow and down arrow moves your paddle. Lets say one player presses the up arrow. I send a packet that does what you'd think it does: "Up" - Just for simplicity at this point I would send that. The server then goes throw a list of possible 'tags' the message can have and handles it accordingly. It will then send back a message to both clients telling it to update the position of one of the paddles. Truthfully I cannot think of any other type to do it (without a similar process) but having to tag each message and sending it to the server which has to search through code on how to handle it. I'm most likely just trying over-complicate this and if that's so, just tell me.

Share this post


Link to post
Share on other sites
Kylotan    9995
That sounds fine to me. All such systems essentially do the same thing, because no matter what you do, the recipient just gets a lump of data and has to work out what the hell it is.

More advanced systems would typically replace the use of ASCII strings like "up" and instead sent a command number or some other compact identifier. For example, sending the command ID as a 16-bit int could allow you 65536 different commands.

PS. I notice you're using the word "packet". I hope you're using UDP. If you're using TCP, then you have no packets as such. Just a heads-up.

Share this post


Link to post
Share on other sites
Sirisian    2263
For a game of pong that's probably the ideal way. I usually use an "enum" like:

class ClientEvents
{
public:
const static unsigned short INPUT_MOVEMENT = 1;
};
and:
class ServerEvents
{
public:
const static unsigned short PADDLE_1_UPDATE = 1;
};

Then just write the bytes to a byte buffer along with the other data. Key input is really cheap to send so you can just use a bitvector.
up = 0x1
down = 0x2
...

Just don't use strings for this stuff. It's really pointless. Just use binary.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this