Polymorphic fail

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Polymorphic fail

Post by Ginto8 »

Okay. Now I think that I understand your problem.

Here's what you should do:
  • 1. create an overall class (maybe a singleton) to do stuff
    2. make the overall class a friend of all the other classes
    3. give the overall class instances of the different classes
    4. give the overall class get functions for all the variables in the different classes
    5. use the overall class's functions for updating
example:

Code: Select all

class Overall // main class (singleton in this example)
{
    A A_instance;
    B B_instance;
    static Overall *instance;
    Overall();
    public:
       int get_a();
       int get_b();
       static Overall *get_instance();
};
Overall overall;

class A
{
    int a;
    friend class Overall;
    public:
       void update();
};

class B
{
    int b;
    friend class Overall;
    public:
       void update();
};

Overall *Overall::get_instance()
{
    if( !instance )
    {
       instance = new Overall;
    }
    return instance;
}

int Overall::get_a()
{
    return A_instance.a;
}

int Overall::get_b()
{
    return B_instance.b;
}

void A::update()
{
    int temp = overall.get_b();
    // do updating stuff
}

void B::update()
{
    int temp = overall.get_a();
    // do updating stuff
}
If you're confused about singletons, go here.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Re: Polymorphic fail

Post by JS Lemming »

Ginto8 wrote:Okay. Now I think that I understand your problem.

Here's what you should do:
  • 1. create an overall class (maybe a singleton) to do stuff
    2. make the overall class a friend of all the other classes
    3. give the overall class instances of the different classes
    4. give the overall class get functions for all the variables in the different classes
    5. use the overall class's functions for updating
Haha, that's pretty clever Ginto8. But in the end that means I would need to have lists containing instances of my various objects in the overall class. Solves the problem at hand but not very practical. I'd be better off just making the lists in my stage class and passing the instance of the stage to all new stage objects I create, in turn, giving them access to one another. That's what I plan to do unless anyone else comes up with anything.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Polymorphic fail

Post by Ginto8 »

JS Lemming wrote:
Ginto8 wrote:Okay. Now I think that I understand your problem.

Here's what you should do:
  • 1. create an overall class (maybe a singleton) to do stuff
    2. make the overall class a friend of all the other classes
    3. give the overall class instances of the different classes
    4. give the overall class get functions for all the variables in the different classes
    5. use the overall class's functions for updating
Haha, that's pretty clever Ginto8. But in the end that means I would need to have lists containing instances of my various objects in the overall class. Solves the problem at hand but not very practical. I'd be better off just making the lists in my stage class and passing the instance of the stage to all new stage objects I create, in turn, giving them access to one another. That's what I plan to do unless anyone else comes up with anything.
One way to do the lists: create a vector of each type of object, and use push_back() to add more. For ease of removal, dynamically allocate the instances.

PS:
Aio, quantitas magna frumentorum est.
Why are you saying "Yes, that is a very large amount of corn" in Latin?
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Re: Polymorphic fail

Post by JS Lemming »

I use lists for my lists.
Ginto8 wrote:Why are you saying "Yes, that is a very large amount of corn" in Latin?
Frito-Lay pays me 8 cents every time somebody reads it.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Polymorphic fail

Post by Ginto8 »

JS Lemming wrote:Frito-Lay pays me 8 cents every time somebody reads it.
Have you ever thought of hiring a personal phsychologist?
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Polymorphic fail

Post by MarauderIIC »

Collisions only happen in a level, so perhaps maintain your different object lists in a Level class. Have your System or Level run a CheckCollisions() on the different lists (Player [Actor], Enemies [Actor], Objects) ?

Don't make something a friend of EVERYTHING if you can help it! Use accessors.

Code: Select all

class Thing { //need better class name
 int x, y, w, h;
 public:
 getX getY getW getH
 bool collided();
};

class Enemy : public Thing { };
class Object : public Thing { };
class Level {
 list<Enemy> enemies;
 list<Object> objects;
 Player player;

 public:
 CheckCollisions();
};

Level::CheckCollisions() {
 for each (enemy in enemies)
  if player.collided(enemy);
   hurt player
 for each (object in objects)
  if player.collided(object)
   stop player
}

Thing::collided(Thing with) {
 if (this->coordinates are inside or touching with's bounding box)
  true
 false
}
Alternatively, remove Thing (or name it better!) override collided(), put collided in Player
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Re: Polymorphic fail

Post by JS Lemming »

Thanks for all the input guys. I could do that Mar, but I think I'd rather have the collision code in the actual objects because different objects will have very different responses to collisions. I'll come back if I fall on my face though. :)

EDIT: Just realized you said override, Mar.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Polymorphic fail

Post by MarauderIIC »

Well, all the level objects would have the same collision code, so hence 'Thing' -- although that's too abstract for me :P I'd prefer Player::collide(Object) Player::collide(Enemy)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Re: Polymorphic fail

Post by JS Lemming »

Hmmm I wonder if I could typecast my stageObject pointer to, say, a hector pointer... maybe that would allow me access to hector variables. I guess it depends how C++ handles everything. I doubt it works that way.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Polymorphic fail

Post by MarauderIIC »

I think you can go up but not down?

IE
(Object)Hector

but not
(Hector)Object
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Re: Polymorphic fail

Post by JS Lemming »

That makes sense.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Post Reply