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

Author Topic: Extracting polymoprhic object from packet.  (Read 2243 times)

0 Members and 1 Guest are viewing this topic.

enigma22134

  • Newbie
  • *
  • Posts: 13
    • View Profile
Extracting polymoprhic object from packet.
« on: August 02, 2016, 08:49:29 pm »
I'm overloading the packet class and running into a bit of an issue.
I have an Actor class that many classes will inherit from; e.g. player class and enemy class.

I would like to extract a generic Actor class from a packet.

packet >> actorObject;

However, if the object is a derived class, then I believe it will be truncated (or crash?) if it is assigned to a super class object.

What I would like to do is get a pointer to what is to be extracted, and do a dynamic cast to check the type of object;

Actor* temporary = &(packet >> ? ? ? ? )
derived* casted = dynamic_cast<derived>(temporary)
if(derived != nullptr){
//do stuff
}


The problem is, I'm not sure how I can do this without knowing the type of the object before extracting it from the packet. I could solve this problem by extracting a flag value first, but then I would need if statments for every type of object.

Does anyone have any input?

Thanks. :)

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Extracting polymoprhic object from packet.
« Reply #1 on: August 03, 2016, 10:30:22 am »
Quote
I could solve this problem by extracting a flag value first, but then I would need if statements for every type of object.

That's how I'd do it (and actually did it, once, a long time ago). Unless you find a (nice, safe, ...) way to serialise a function and send it through the network, you're stuck with sending raw data.
SFML / OS X developer