We are talking C++??
I assume AI inherits from creature?? That down not sound right. Creature should inherit from AI. Anyway you need to proably give us some sort of class information.
Umm, I am sure you should NOT be deleting this while you are in a function of this. Sounds very dangerous to me. Although it is valid code, I am guessing your this object was not created with new. If you created your object with new it should not crash, but is a bad idea generally.
Both Player and AI inherit from Creature which inherits from Entity.
The object was created with
new. The crash wasn't actually happening at the delete statement, though my pathetic explanation made it sound that way. The crash was happening later when the Level was looping through the creatures and drawing them. Since my removeCreature() function was messed up, the wrong creature got removed from the list and the pointer to the deleted one stayed, so the function that draws creatures to the screen was trying to access something that wasn't there.
I read that for every
new statement, one should have a
delete statement to free the memory allocated. Since creatures is a vector of pointers to Creature objects, should I be deleting them when the level is destroyed?
Level::~Level()
{
// Actual code from my game
delete [] map;
delete [] flagmap;
// Should I do the following?
for(std::vector<Creature*>::iterator it = creatures.begin();it!=creatures.end();++it)
{
delete creatures[i];
}
creatures.clear();
}
The level is the only thing that ever points to creatures, but that might change. Perhaps I should make the Dungeon class's (represents the whole game; has a linked list of Level objects) destructor go through all its Level objects and make them delete their creatures. It doesn't seem right to have a bunch of dead creatures taking up memory, but I don't think it matters too much in a little project like this.
for(std::vector<Creature*>::iterator it = creatures.begin();it!=creatures.end();++it)
{
if( *it == remove)
{
creatures.erase(it);
return;
}
}
This fixes the problem, thanks! I hope you don't mind if I just copy/paste.
Hey, can you post your player->addMessage() code.
That looks like something I should be doing. I am still using c style
i.e. addMessage(char * ...);
as my sig demonstrates.
I have been meaning to look into upgrading to c++ messges, but if you have already done it...
Are you sure you want my code after seeing some of it?
void Player::addMessage(std::string message)
{
messBuffer.push_back(message);
messList.push_back(message);
}
Both messBuffer and messList are vectors of strings in the Player class. messBuffer stores the recent messages that the player hasn't seen yet and is emptied after they are displayed. messList stores all messages received throughout the game so the player can review them later.
void Player::displayMessages()
{
if(messBuffer.size() > 0)
{
int linePos=0; // horizontal position of the current message
for(int i=0; i<messBuffer.size(); i++)
{
mvaddstr(0,linePos,messBuffer[i].c_str());
linePos += messBuffer[i].length() + 1; // Move the position of the line forward the length of the message and add some space.
if(messBuffers.size() > i+1 && linePos+messBuffer[i+1].length() > COLS-6) // If the next message is longer than the screen and the 6 characters needed to add "(more)"....
{
mvaddstr(0,linePos,"(more)");
getch(); // Wait for the player.
// Redraw the screen.
erase();
getLevel()->print();
attron(COLOR_PAIR(getColor()));
mvaddch(getY(),getX(),getSymbol());
linePos=0;
}
}
messBuffer.clear();
}
}
I hope that helps, but I doubt it, considering how much more experienced you are.
I'm using pdcurses. What are you using?