Archived

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

Etus

Designing a Monsters(entities) List class

Recommended Posts

Etus    192
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
grbrg    126
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   
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
petewood    819
quote:
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) {
monsters_(monsters);
}
operator()(Monster& monster) {
monsters_.push_back(&monster);
}
}

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

zombies.push_back(Zombie("Rod"));
zombies.push_back(Zombie("Jane"));
zombies.push_back(Zombie("Freddy"));
ghouls.push_back(Ghoul("George"));
ghouls.push_back(Ghoul("Zippy"));
ghouls.push_back(Ghoul("Bungle"));
vampires.push_back(Vampire("Mr."));

for_each(zombies.begin(), zombies.end(),
addMonsterToMonsterCont(monsters));
for_each(ghouls.begin(), ghouls.end(),
addMonsterToMonsterCont(monsters));
for_each(vampires.begin(), vampires.end(),
addMonsterToMonsterCont(monsters));

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
Etus    192
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:
http://corky.net/rpg/monsters.zip

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);
MonMan.LoadMonster();
Monster_Temp.push_back(MonMan);

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".

Thanks,
Yuval

Share this post


Link to post
Share on other sites