Archived

This topic is now archived and is closed to further replies.

Jawbreaker

Creating and Tracking Enemies

Recommended Posts

I want to have a base class called cEnemy lets say and a bunch of child classes for different types of enemies. Now I want to make a vector to keep track of all enemies such as for collision testing. Can I make a universal vector of cEnemy or something, I dont want to have to create a seperater vector for each type of enemy. Hope this makes sense. Also if theres a more standard way of doing this Im open to suggestions, thanks. Jawbreaker

Share this post


Link to post
Share on other sites
Aye, theoretically you should be able to create a vector for the base class and store any cEnemies you want in there... (am I right on this? I think so) Since any descendants of cEnemy are also a cEnemy itself.

EDIT: Aye, that's a good idea, the pointer idea =)

[edited by - Vthornheart on March 20, 2004 4:11:18 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by VThornheart
Aye, theoretically you should be able to create a vector for the base class and store any cEnemies you want in there... (am I right on this? I think so) Since any descendants of cEnemy are also a cEnemy itself.


Bad, bad idea. Creating a container that stores a base object by value will lead to slicing.

Share this post


Link to post
Share on other sites
So basically what you want to do is something like

std::vector< cEnemy* > m_vecEnemies;

You want to store pointers to your base object. Say you have a cRat that inherits from cEnemy. You could add a rat to your list of enemis by doing this:

cRat* pNewRat = new cRat();
// init member variables for your new "rat"
m_vecEnemies.push_back( pNewRat );

Hope this helps,
neneboricua

[edited by - neneboricua19 on March 20, 2004 4:54:23 PM]

Share this post


Link to post
Share on other sites