Jump to content
  • Advertisement


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


Designing a Monsters(entities) List class

This topic is 5561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a problem. I''ve decided to introduce entities to my engine, and I want to have an entities list using STD. This way I can remove a specific entity, render them all, etc. At start, I want only to insert monsters into that list. My monsters classes are built like this: Monsters(Base class) Zombie(Abstracted from monsters) Ghoal(Abstracted from monsters) ... I want to be able to add to the monsters list all kinds monsters I have. I can''t do this, for example:
typedef vector<Monsters> pMonstersList;
pMonstersList MList; 
This way I can''t add any monster. I can only add a Monsters variable, but I don''t want to do that because Monsters is my base class. I can''t do this either:
typedef vector<Zombie> pZombieList;
pZombieList ZList; 

typedef vector<Ghoal> pGhoalList;
pGhoalList GList; 
This way I''ll need to create a list to each different monster, and that''s not good. How can I create a general list, one that can contain any monster I want? Thanks for you help, Yuval

Share this post

Link to post
Share on other sites
Aside from the point that you should call the type MList and the variable pMonsterList it should work - if Monster is real the base class. You may have to cast it to Monster if the compiler does not recognize that child... Sorry for the question, but are you sure everything is alright and it doesn''t work...?

"Reality is nothing, perception is everything!" - First Wizard Zeddicus Zu''l Zorander

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Use a vector of pointers to the base class.

typedef vector<Monsters*> pMonstersList;

Share this post

Link to post
Share on other sites
grbrg:it should work - if Monster is real the base class. You may have to cast it to Monster if the compiler does not recognize that child

no, the list is of Monster objects which take up a certain size. You can't try and put something larger in that space (ie something derived). Only a class of exactly type Monster will fit.

You'll need to store pointers to the base Monster and look after the memory managment yourself.

edit: There's no reason not to have specific containers of derived monsters as you were saying and then storing pointers to all of them in the container as the AP said

// typedef the different monster containers - these can easily be changed to lists, deques etc.

typedef vector<Zombie> ZombieCont;
typedef vector<Ghoul> GhoulCont;
typedef vector<Vampire> VampireCont;

typedef list<Monster*> MonsterCont;

// function object used later

struct addMonsterToMonsterCont {
MonsterCont& monsters_;
addMonsterToMonsterCont(MonsterCont& monsters) {
operator()(Monster& monster) {

ZombieCont zombies;
GhoulCont ghouls;
VampireCont vampires;
MonstersCont monsters;


for_each(zombies.begin(), zombies.end(),
for_each(ghouls.begin(), ghouls.end(),
for_each(vampires.begin(), vampires.end(),

You now have a list of pointers to all the monsters in their seperate containers. When you've finished the MonsterCont will just delete its pointers (not the pointed to objects). The other containers will delete their contents.

Sorry it's not a complete demo. If you have any questions or problems please ask.

[edited by - petewood on March 6, 2003 11:47:25 AM]

Share this post

Link to post
Share on other sites
First of all - thanks a lot petewood for your answer.

I still have the same problem - when I try to access _monsters I get the functionality of the abstract class only, and not the monster class itself. I think you need to see my source code in order to understand:

You may download the Monsters, MonsterTemp and MonstersList source code here:

This is how I declare and use everything in the main file:



// Monsters


MonstersList* MonList;
MonstersCont MainMonsters;
MonsterTempCont Monster_Temp;
int monsterNum = 0;


// Monster

CiVector3 sendPoints(20, 21, 22);
CVector3 MonsterStartingPos = TerMan.ReturnPoint(sendPoints);
MonsterStartingPos.y += 20.0f;

MonsterTemp MonMan(&TexMan, MonsterStartingPos);

MonstersList* MonListTemp = new MonstersList(Monster_Temp);
MonList = MonListTemp;

// End monster

When I try access monsters_ like this:

MonList->monsters_[0]-> ...

I get the functionality of "Monsters" and not "MonsterTemp".


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!