Dear all,
I'm writing a simple 2d sprite game in my spare time. I'm fairly experienced in C++ and I'm running into unknown and unexpected problems with a vector of mine. I have looked on forums and around the web and I cannot find any help towards a solution so I am posting here. This is my first post on this forum so if I do something completely taboo... please forgive me.
So here's the problem: The game contains an std::vector called the "EntityList" This is a vector of pointers to all the entities in the game (enemies, PC's, NPC, Chest... etc) The entity List is where all the collision detection is handled, the calls to render and all that happy fun stuff.)
So I wanted to create a "list" of enemies, that would be dynamic, that is to say it would vary in size based on the map the player was on. (either 1 big boss, or 50 little monsters) and I decided that the best way to do it would be a vector. So...
std::vector<CEnemy> EnemyList;
and then the Enemylist would be "pushed" into the entitylist after it is filled up and then as the enemies die, they are taken out of the EntityList. Then when the player goes to a new Map... Take all the remaining enemies out of the Entity List... then clear the Enemylist, and reload the EnemyList (thats the idea at least.)
NOTE: EntityList = vector of pointers to actual objects in "EnemyList." EnemyList is suppose to contain actual CEnemy's.
Ok so here's the code that's going berserk. It is suppose to (while loading the map to be displayed, also...) load information from the .map file in the format of "x:y " where x is the type of enemy and Y is the number of that enemy on the map... To be read from the Enemy.data file later on... Here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
//declared in class declaration
std::vector<CEnemy> EnemyList; //Enemy List.
//-------------
//Earlier in Game.OnInitilize()
EnemyList.reserve(50);
//---------------
bool CArea::OnLoad(char* File) {
MapList.clear();
for (int i = 0; i < Game.EnemyList.size(); i++){
Game.EnemyList[i].OnCleanup();
}
Game.EnemyList.clear();
int Area_Enemy; //The Type of Enemy in the area (range 1-255)
int En_Num; //# of those enemies
char TilesetFile[255]; //File name for loading tileset for map.
FILE* FileHandle = fopen(File, "r");
if(FileHandle == NULL) {
return false;
}
fscanf(FileHandle, "%s\n", TilesetFile);
if((Surf_Area_Tileset = CSurface::OnLoad(TilesetFile)) == false) {
fclose(FileHandle);
return false;
}
fscanf(FileHandle, "%d\n", &AreaSize); //Needed for Map Loading
fscanf(FileHandle, "%d:%d ", &Area_Enemy, &En_Num); //"x:y " X = types of enemies, y = number of enemies of type X
//Creates and pushes an new Enemy into Enemy List with with ID = Area_Enemy, En_Num times...
while (Area_Enemy != -1) {
CEnemy Etemp;
Etemp.ID = Area_Enemy;
for (int i = 0; i < En_Num; i++) {
//THIS IS WHERE IT ALL GOES BAD!!!!-----
Game.EnemyList.push_back(Etemp);
}
fscanf(FileHandle, "%d:%d ", &Area_Enemy, &En_Num);
}
fscanf(FileHandle, "\n");
// THIS IS ALL MAP LOADING SEQUENCES... THIS ALL WORKS FINE (To my knowledge)
for(int X = 0; X < AreaSize; X++) {
for(int Y = 0; Y < AreaSize; Y++) {
char MapFile[255];
fscanf(FileHandle, "%s ", MapFile);
CMap tempMap;
if(tempMap.OnLoad(MapFile) == false) {
fclose(FileHandle);
return false;
}
tempMap.Surf_Tileset = Surf_Area_Tileset;
MapList.push_back(tempMap);
}
fscanf(FileHandle, "\n");
}
// THIS IS END OF MAP LOADING SEQUENCE ---------------------
fclose(FileHandle);
AreaControl.LoadEnemyList(); //This is where it goes to the Enemy.data file and loads the information based off of the Enemy's ID
return true;
}
|
So after debugging a number of times and stepping through the code I've discovered this is what is happening:
(In this example "x:y " from file is, X = 1, y = 5, so EnemyList.size() is suppose to = 5 (which is does) and Enemylist[all].ID is suppose to = 1)
EnemyList's Capacity is ending up = 52 (not that big a concern... why I don't know though... but not that big of a concern at the moment)
EnemyList[0].ID = 1
EnemyList[1].ID = -865262626
EnemyList[2].ID = 2
EnemyList[3].ID = 0
EnemyList[4].ID = 0 ...etc...
So the first one is correct and each one after that is screwed up.
On
Game.EnemyList.push_back(Etemp);
Etemp's ID does = 1, but somewhere in the .push_back() it alters or corrupts.
Another thing to note. CEnemy Class does inherit CEntity Class in its definition. I don't know if that will matter or not but...
Also I've come across an error happening later in my code where on
std::string StringName
I'm getting a "Corruption of the heap" error. I have a gut feeling that the two are related but for the life of me I can't figure out why or how.
Can anybody take a look at this and let me know?
Note: This is a pretty hefty sized program at the moment and before I implemented this "enemylist" code everything worked fine (no symptoms at least) so I've just included that code. If there is any other code you would like to see please let me know and I will gladly post it. Thank you very much.
- Jutebox5