Page 2 of 2

Re: Polymorphic fail

Posted: Sun Jan 18, 2009 1:27 pm
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.

Re: Polymorphic fail

Posted: Sun Jan 18, 2009 2:22 pm
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.

Re: Polymorphic fail

Posted: Sun Jan 18, 2009 2:28 pm
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?

Re: Polymorphic fail

Posted: Sun Jan 18, 2009 2:39 pm
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.

Re: Polymorphic fail

Posted: Sun Jan 18, 2009 3:36 pm
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?

Re: Polymorphic fail

Posted: Sun Jan 18, 2009 7:32 pm
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

Re: Polymorphic fail

Posted: Sun Jan 18, 2009 9:35 pm
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.

Re: Polymorphic fail

Posted: Mon Jan 19, 2009 2:21 pm
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)

Re: Polymorphic fail

Posted: Mon Jan 19, 2009 4:02 pm
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.

Re: Polymorphic fail

Posted: Mon Jan 19, 2009 4:05 pm
by MarauderIIC
I think you can go up but not down?

IE
(Object)Hector

but not
(Hector)Object

Re: Polymorphic fail

Posted: Mon Jan 19, 2009 4:11 pm
by JS Lemming
That makes sense.