I'm working on a roguelike game and I have it set up so that at MOST there will be 3 monsters on a level. So I have 3 pointers to monsters. They all start out as NULL.
How should I go about setting the pointer back to NULL after that monster dies? Or maybe I have the wrong idea? Essentially I need the monster to be removed until that pointer is regenerated as another monster.
I've tried a few things but I just keep getting seg faults.
I tried using erase, delete, simply setting it to NULL, and repointing it. Can't figure it out.
Here's how I set the pointers:
CreatureFactory myCreatures = CreatureFactory::instance();
Creature * pCreature1 = NULL;
Creature * pCreature2 = NULL;
Creature * pCreature3 = NULL;;
ItemFactory myItems = ItemFactory::instance();
Item * pItem1 = myItems.generateItem();
dl.placeInGame(personPlaying,randomGen);
int monsterCount = 0;
int turnCount = 0;
bool playingGame = true;
char playerController;
while (playingGame){
if ((turnCount % 2 == 0) && (personPlaying.getHP() < personPlay$
personPlaying.generateHP();
}
if (((turnCount % 3) == 0) && (monsterCount < 2)){
if(pCreature1 == NULL){
pCreature1 = myCreatures.generateCreature(4);
dl.placeInGame(pCreature1,randomGen);
monsterCount += 1;
}
else if(pCreature2 == NULL){
pCreature2 = myCreatures.generateCreature(4);
dl.placeInGame(pCreature2, randomGen);
monsterCount += 1;
}
else if(pCreature3 == NULL){
pCreature3 = myCreatures.generateCreature(4);
monsterCount += 1;
}
}
dl.dump();
personPlaying.dumpStats();
CreatureFactory code..
CreatureFactory & CreatureFactory::instance(){
static CreatureFactory myObj;
return myObj;
}
CreatureFactory::CreatureFactory(){
m_mtRandom.seed( time(NULL) );
fstream xmlFile;
xmlFile.open("critters.xml");
vector<XMLSerializable*> pObjects;
parseXML(xmlFile, pObjects);
XMLSerializable * pObject;
for(auto it = pObjects.begin(); it != pObjects.end(); it++){
pObject = (*it);
Creature * pCreature = dynamic_cast<Creature*>(pObject);
if (pCreature != NULL){
m_vCreatures.push_back(pCreature);
}
}
}
CreatureFactory::~CreatureFactory(){
}
Creature * CreatureFactory::generateCreature(int iMaxLevel) {
vector<Creature*> tempCreatures;
for(auto it = m_vCreatures.begin(); it != m_vCreatures.end(); it++){
if ((*it)->getLevel() <= iMaxLevel){
tempCreatures.push_back((*it));
}
}
int randomCreature = (m_mtRandom() % (tempCreatures.size() - 1));
Creature * pCreature = tempCreatures.at(randomCreature);
Creature * pReturnValue = new Creature(*pCreature);
return pReturnValue;
}
Then I do a ton of looping and use tons of classes. The pointers to these monster objects get passed through about 6 or 7 classes.. one of which I need to set to NULL when its HP hits 0.
Thanks