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

Author Topic: Packets : Vector Transfers and sf::objects  (Read 6321 times)

0 Members and 1 Guest are viewing this topic.

r4gTime

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Packets : Vector Transfers and sf::objects
« on: April 29, 2020, 03:54:26 pm »
Hello,

I feel like I'm  kind alone on this forum but I still try :)

I have two question :

1- Is it possible to send a structure (or whatever) with vectors in it by packet >> ?
Like this :
typedef struct BMMaps BMMap;
        struct BMMaps
        {
            std::string nom = "Map par défaut" ;
            int background = 0;
            std::vector<Plateforme> plateformes;

        };
inline sf::Packet& operator <<(sf::Packet& packet, const BMMap& m)
{
    return packet << m.nom << m.background << m.plateformes ;
}
inline sf::Packet& operator >>(sf::Packet& packet, BMMap& m)
{
    return packet >> m.nom >> m.background >> m.plateformes ;
}
 

This doesn't work of course, and when I try to send the size of the vector then every element in it with a for(i=0; i < myVector.size() ; i++) , it gives me an error as well .

2- Is it possible to send sf::objects in packet ?
I'd like to send a lot of sf::RectangleShape , or CircleShape, but this :
sf::RectangleShape r;
sf::Packet p << r;
 
doesn't work. I mean, I guess sfml made its own packet object compatible with its own others objetcs ?


Thanks for your help,

Cya

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Packets : Vector Transfers and sf::objects
« Reply #1 on: April 29, 2020, 04:34:07 pm »
Hi

1. std::vector<Plateforme> is a custom type, so if you want to support it, you have to overload stream operators, just like you did with BMMap. A template function that works for any std::vector<T> would be a good exercise ;)

Quote
it gives me an error as well
This is useless as long as you don't give the exact error message that you get.

2. Same answer. To support a custom type you have to write a pair of overloaded stream operators. No, sf::Packet doesn't support SFML classes as it makes very little sense to send a graphical entity through the network (but if you need to, that's ok -- with your own overloads).
Laurent Gomila - SFML developer

r4gTime

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Packets : Vector Transfers and sf::objects
« Reply #2 on: April 29, 2020, 07:35:30 pm »
Hello, thank you for your answer.

I didn't try for vectors actually, only for maps.

My overload operator for vectors seems to work (I don't know how to use templates, I'll try someday) :
inline sf::Packet& operator <<(sf::Packet& packet, const std::vector<Plateforme>& p)
{
    packet << p.size();
    for(int i = 0; i < p.size();  i++) packet << p.at(i) ;
    return packet;
}
inline sf::Packet& operator >>(sf::Packet& packet, std::vector<Plateforme>& p)
{
    int n;
    Plateforme x;
    packet >> n;
    p.clear();
    for(int i =0; i < n ;i++) { packet >> x; p.push_back(x);}
    return packet;
}

But for maps, I get an error whenever I try to use an iterator in the function :
inline sf::Packet& operator <<(sf::Packet& packet, const std::map<std::string,int[20][2]>& a)
{
    std::map<std::string,int[20][2]>::iterator it = a.end(); // Get length of the map : ERROR
    // Rest of the code
}

Error : conversion from 'std::map<std::__cxx11::basic_string<char>, int [20][2]>::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::__cxx11::basic_string<char>, int [20][2]> >}' to non-scalar type 'std::map<std::__cxx11::basic_string<char>, int [20][2]>::iterator {aka std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, int [20][2]> >}' requested|

Anyway I'm not sure this is related to SFML. But this works normally outside the overload operator.

Anyway, thanks for your help !

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Packets : Vector Transfers and sf::objects
« Reply #3 on: April 29, 2020, 10:49:21 pm »
A const std::map<>& cannot give you a non-const iterator ;)
Laurent Gomila - SFML developer

r4gTime

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Packets : Vector Transfers and sf::objects
« Reply #4 on: April 30, 2020, 11:53:03 am »
It seems std::map doesn't want me to send datas. This code compiles but the app shuts down at "packet << s1" :
inline sf::Packet& operator <<(sf::Packet& packet, const std::map<std::string,sf::Int16>& a)
{
    std::string s1;
    for(auto& x : a) {
        std::cout << x.first << std::endl ; // Works, displays the right string
        s1 = (std::string) x.first;  // I tried everything but
        packet << s1 ; // My app shuts down, packet << x.first doesn't work either
    }
    return packet;
}

Thank you again for your help :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Packets : Vector Transfers and sf::objects
« Reply #5 on: April 30, 2020, 01:21:39 pm »
Quote
(std::string) x.first
A C-style cast is always a bad idea. A C-style cast to a non-primitive type is always a crash ;)
However in your case... x.first is already a std::string, isn't it?

When your app crashes, you can use the debugger. It gives plenty of useful information.
Laurent Gomila - SFML developer

 

anything