How can I send coordinates over Enet?

Started by
4 comments, last by yaboiryan 3 years, 11 months ago

Heya,

So I was making a multiplayer game engine demo that I hoped would look similar to Goldeneye.

I got connections working, but I ran into the issue of sending more than one coordinate at a time.

Here is a video of the demo:

As you can see, I can only send the character's X coordinate.

My overall question is how I can send more than one packet at a time or send X, Y, and Z coordinates in one packet.

Thank you in advance,

-Ryan

Advertisement

You need to create a higher-level structure of entity state, bundle it all up, and send it as a single thing.

For example, you could have a structure for your player that looks something like:

struct PlayerStatePacket {
    float pos_x;
    float pos_y;
    float pos_z;
    float vel_x;
    float vel_y;
    float vel_z;
    float look_right;
    float look_up;
    float aim_right;
    float aim_down;
    uint32_t control_bitmask; // walking/running/strafing/crouching/....
    uint32_t num_shots_fired;
    uint32_t num_grenades_thrown;
};

And you send it like so:

PlayerStatePacket psp = { ... };
ENetPacket * packet = enet_packet_create (&psp, sizeof(psp), 0);
/* Send the packet to the peer over channel id 0. */
enet_peer_send (peer, 0, packet);

Your receiving end would have to know that channel 0 means “the player state packet of this entity." It could then extract this struct again, and update whatever needs updating on the remote end.

Once you've made this work, you will likely want to add a little more structure. For example, you might add two uint16_t at the beginning of the struct, for “which player is this for” and “what kind of packet is this?” (and start every packet with those two fields, so you can tell them apart when receiving.)

Once you have all of that working, you can start working on compressing the size of the packet, using rounding, bit packing, delta encoding, and so forth – but, honestly, those are not super important for a simple modern networked game. It's more important to get gameplay and interpolation/extrapolation right!

enum Bool { True, False, FileNotFound };

@hplus0603 Thank you for your reply! My last issue is how I am gonna convert the packet data to the format of the PlayerStatePacket struct. Should I just create another PlayerStatePacket variable, or is there some other way I can do that.

Network programming is a fairly intense systems programming subject.

To succeed, you need to understand how your language works, what bits and bytes are, what pointers are, what buffer copy versus references are, what asynchronous events are, and many more subjects.

If you are having trouble with these concepts, then I suggest that trying to do networking might not yet be right for you – networking is hard enough when you speak the language, but if you're trying to learn C/C++ at the same time you're trying to learn networking, you are setting yourself up for failure. Try to tackle only one complex thing at a time!

enum Bool { True, False, FileNotFound };

@undefined Thank you for your advice! I found what the problem was and how to solve it! have a nice day and stay safe!

This topic is closed to new replies.

Advertisement