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

Author Topic: Quick question, Sending Lists or Vectors through Packets  (Read 5257 times)

0 Members and 1 Guest are viewing this topic.

cooldog99

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
    • Perilous Game Studios
Quick question, Sending Lists or Vectors through Packets
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Quick question, Sending Lists or Vectors through Packets
« Reply #1 on: April 26, 2011, 08:59:51 pm »
Send:
Code: [Select]
packet << static_cast<sf::Uint32>(list.size());
for (std::list<xxx>::const_iterator it = list.begin(); it != list.end(); ++it)
    packet << *it;


Receive:
Code: [Select]
sf::Uint32 size;
packet >> size;
for (sf::Uint32 i = 0; i < size; ++i)
{
    xxx item;
    packet >> item;
    list.push_back(item);
}
Laurent Gomila - SFML developer

cooldog99

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
    • Perilous Game Studios
Quick question, Sending Lists or Vectors through Packets
« Reply #2 on: April 26, 2011, 09:04:43 pm »
Quote from: "Laurent"
Send:
Code: [Select]
packet << static_cast<sf::Uint32>(list.size());
for (std::list<xxx>::const_iterator it = list.begin(); it != list.end(); ++it)
    packet << *it;


Receive:
Code: [Select]
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

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Quick question, Sending Lists or Vectors through Packets
« Reply #3 on: April 26, 2011, 09:29:13 pm »
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.

cooldog99

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
    • Perilous Game Studios
Quick question, Sending Lists or Vectors through Packets
« Reply #4 on: April 26, 2011, 09:31:39 pm »
Quote from: "Mjonir"
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Quick question, Sending Lists or Vectors through Packets
« Reply #5 on: April 26, 2011, 09:36:10 pm »
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
Laurent Gomila - SFML developer

cooldog99

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
    • Perilous Game Studios
Quick question, Sending Lists or Vectors through Packets
« Reply #6 on: April 26, 2011, 09:38:56 pm »
Quote from: "Laurent"
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.

 

anything