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

Author Topic: Trouble receiving ints in classes from packets  (Read 3798 times)

0 Members and 1 Guest are viewing this topic.

KingPenguinator

  • Newbie
  • *
  • Posts: 21
    • View Profile
Trouble receiving ints in classes from packets
« on: June 04, 2014, 03:45:17 pm »
Hi

So I have this problem that has been bugging me these past few days, i've searched a fair bit, but to no avail.

When I do this:

//sockets and packets already declared

sf::Vector2i playerPos;

socket.receive(packet);
if(packet >> playerPos.x >> playerPos.y)
{
      player.setPosition(playerPos);
}

 
I'll get an overload error saying that the compiler doesn't know how to cast int to char*, I've heard somewhat that I need to make it explicit(I don't know how/where).

Any help would be appreciated, if you need more code, do say so

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Trouble receiving ints in classes from packets
« Reply #1 on: June 04, 2014, 03:50:50 pm »
Please show the exact error message, as well as the line of code where the compilers points to.
Laurent Gomila - SFML developer

KingPenguinator

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Trouble receiving ints in classes from packets
« Reply #2 on: June 04, 2014, 06:48:19 pm »
sf::Packet& operator >>(sf::Packet& packet, Personnage& player)
{
    return packet >> player.getPos().x >> player.getPos().y;
}

fighter - client\game.cpp||In function 'sf::Packet& operator>>(sf::Packet&, Personnage&)':|
fighter - client\game.cpp|20|error: no match for 'operator>>' in 'packet >> (& player)->Personnage::getPos().sf::Vector2<float>::x'|
fighter - client\game.cpp|20|note: candidates are:|
SFML-2.1\include\SFML\Network\Packet.hpp|177|note: sf::Packet& sf::Packet::operator>>(bool&)|
SFML-2.1\include\SFML\Network\Packet.hpp|177|note:   no known conversion for argument 1 from 'float' to 'bool&'|
SFML-2.1\include\SFML\Network\Packet.hpp|178|note: sf::Packet& sf::Packet::operator>>(sf::Int8&)|
SFML-2.1\include\SFML\Network\Packet.hpp|178|note:   no known conversion for argument 1 from 'float' to 'sf::Int8& {aka signed char&}'|
 

I couldn't find the original snippet that I 1st posted about, but I have the same problem with this
« Last Edit: June 04, 2014, 06:52:14 pm by KingPenguinator »

KingPenguinator

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Trouble receiving ints in classes from packets
« Reply #3 on: June 04, 2014, 06:58:49 pm »
I'm not sure, but I think I solved it with this

sf::Packet& operator >>(sf::Packet& packet, Personnage& player)
{
    sf::Vector2i playerPos;
    return packet >> playerPos.x >> playerPos.y;
}
 

I don't know if that's the solution, or i'm just using a different way of extracting the packet.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Trouble receiving ints in classes from packets
« Reply #4 on: June 04, 2014, 09:12:13 pm »
Could you show us the code where you put data into the packet, and the implementation of Personnage?  Or better yet, a complete and minimal example.  This is probably just a type mismatch somewhere in your code.

KingPenguinator

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Trouble receiving ints in classes from packets
« Reply #5 on: June 05, 2014, 12:54:26 pm »
//implementation, both player2 and player2aux are Personnages
if(packet >> player2aux)
        {
            player2.updatePlayer(player2aux); //updates player2 if player2aux's position is different
        }

//the way i'm using the overload now :
sf::Packet& operator >>(sf::Packet& packet, Personnage& player)
{
    sf::Vector2i playerPos;
    if(packet >> playerPos.x >> playerPos.y)
    {
        player.setPos(playerPos.x, playerPos.y);
    }
    return packet;
}

 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Trouble receiving ints in classes from packets
« Reply #6 on: June 05, 2014, 01:25:34 pm »
You shouldn't use native types directly in packets, because their size can vary between the client and the server. Use fixed-size types instead (sf::Uint32, etc.). This problem, as well as others, are very well described in the tutorial about packets. You should read it carefully.
Laurent Gomila - SFML developer

KingPenguinator

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Trouble receiving ints in classes from packets
« Reply #7 on: June 05, 2014, 01:40:37 pm »
So I should use sf::Int32 for example in all of my classes instead of Int ?

If so, is it possible to cast an int to a sf::Int32 and vice versa ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Trouble receiving ints in classes from packets
« Reply #8 on: June 05, 2014, 01:53:20 pm »
Quote
So I should use sf::Int32 for example in all of my classes instead of Int ?
Use fixed-size types for everything that you put in / extract from packets.

Quote
If so, is it possible to cast an int to a sf::Int32 and vice versa ?
Yes of course, fixed-size types are just typedefs to native types. For example, sf::Uint32 is just a typedef to unsigned int.
Laurent Gomila - SFML developer

KingPenguinator

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Trouble receiving ints in classes from packets
« Reply #9 on: June 05, 2014, 01:56:32 pm »
hmm, could you give me an example of casting it please ?
I don't quite get what you said about typedefs.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Trouble receiving ints in classes from packets
« Reply #10 on: June 05, 2014, 02:23:24 pm »
hmm, could you give me an example of casting it please ?
I don't quite get what you said about typedefs.
You seem to know neither static_cast nor typedefs... You should really learn C++ properly before trying to use SFML ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

KingPenguinator

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Trouble receiving ints in classes from packets
« Reply #11 on: June 05, 2014, 02:26:45 pm »
Well the idea was to learn as I go along, I can't know ahead of time what i'm going to need to have learned before the need to have learned it comes.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Trouble receiving ints in classes from packets
« Reply #12 on: June 05, 2014, 02:28:12 pm »
You know that you need to learn C++ before SFML, obviously ;)
Laurent Gomila - SFML developer

KingPenguinator

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Trouble receiving ints in classes from packets
« Reply #13 on: June 05, 2014, 02:29:22 pm »
Agreed

KingPenguinator

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Trouble receiving ints in classes from packets
« Reply #14 on: June 05, 2014, 02:38:13 pm »
Well, thx for the help.
Now i'm going to have "fun" learning about static_casts and such ;)