SFML community forums
Help => Network => Topic started by: cooldog99 on April 26, 2011, 08:43:06 pm
-
Hey guys, I was just wondering what's the best way to send lists or vectors through a SINGLE packet and have a list = packet contents.
Basically
Server:
Vector of enemies
Send packet of said Vector ^
Client:
Receive Packet
Enemies Vector = Packet Contents
I'm sorta ignorant to Packets, I've used them for single data but not large lists.
SFML 1.6
Windows vista btw.
-
Send:
packet << static_cast<sf::Uint32>(list.size());
for (std::list<xxx>::const_iterator it = list.begin(); it != list.end(); ++it)
packet << *it;
Receive:
sf::Uint32 size;
packet >> size;
for (sf::Uint32 i = 0; i < size; ++i)
{
xxx item;
packet >> item;
list.push_back(item);
}
-
Send:
packet << static_cast<sf::Uint32>(list.size());
for (std::list<xxx>::const_iterator it = list.begin(); it != list.end(); ++it)
packet << *it;
Receive:
sf::Uint32 size;
packet >> size;
for (sf::Uint32 i = 0; i < size; ++i)
{
xxx item;
packet >> item;
list.push_back(item);
}
:O that was fast, although another dumb question (Sorry!)
What if the list or vector contains a class, instead of an int?
Same method?
for instance, Enemy class, contains health, sprite, damage, speed, etc. and there's roughly 20 in a list, I can just use that method above and dynamically push back a new Enemy into the new list after receiving the packet?
Again, thanks for the fast reply, and sorry for being ignorant, I'm slow today :x
-
I don't know anything about networking, but the code Laurent seems to unpack and repack a std::list<xxx>. It's a template, it'll work for any kind of data you'll ask it to send. So if you have a std::list<Enemy>, you will be sending your Enemy classes, not just integers.
-
I don't know anything about networking, but the code Laurent seems to unpack and repack a std::list<xxx>. It's a template, it'll work for any kind of data you'll ask it to send. So if you have a std::list<Enemy>, you will be sending your Enemy classes, not just integers.
Oh, maybe that'll work.
If anyone knows a better way(if any) or if they know whether or not this will work, please post, I will be trying this!
Thanks.
-
SFML packets know how to pack/unpack primitive types and strings, that's all. For anything more complicated, you must tell SFML how to decompose/recompose the complicated things to/from primitive types, like I did above with std::list.
So for your Enemy class, it's the same. Read the "Packets and custom classes" sections of the tutorial, it's explained with an example:
http://www.sfml-dev.org/tutorials/1.6/network-packets.php
-
SFML packets know how to pack/unpack primitive types and strings, that's all. For anything more complicated, you must tell SFML how to decompose/recompose the complicated things to/from primitive types, like I did above with std::list.
So for your Enemy class, it's the same. Read the "Packets and custom classes" sections of the tutorial, it's explained with an example:
http://www.sfml-dev.org/tutorials/1.6/network-packets.php
Alright I think I might know what you mean now.
Thanks again, i'll be trying this soon.