Well another query for the day. A bit long post this time. Please bear with me and help me
I have a class called TEnt which basically defines a entity.
I have another class called Manager which sort of manages the entity. The manager is a singleton class which has the following members
Code:
/*! This is called from other classes to register the instance with the manager*/
void REGISTER_ENTITY_INSTANCE(TEnt& instance);
/*! Call the set entity call in each of the registered entities*/
void EntityCall(void);
/*! Calls a draw entity call to each of the registerdd entity*/
void drawEntities(void);
/*! Prints out a test message*/
void print();
private:
/*! A vector of TEnts*/
std::vector < TEnt* > m_ppTEntList;
///////////////////////////////////////////converting this into a singleton
////////////////////////////////////////////////////////////////////////////
private:
EntityManager(){;}
EntityManager(const EntityManager&){;}
EntityManager& operator= (const EntityManager&){;}
static EntityManager* m_pInstance;
static bool is_InstanceSet;
public:
static EntityManager* getPInstance();
~EntityManager()
{
is_InstanceSet=false;
}
I have a Screen class which displays all the entities on the screen and a LevelEditor class which initialises and registers the entities on to the entity manager.
This piece of code initialises the level and gets a instance of the Manager.
Now This is the error I am facing
When I call the print function in the screen class it works fine .
it says that a entity has been registered and that the size of the vector is now 1. But this is the piece of code that seems to go wrong and creates a SEGMENTATION fault.
Code:
vector<TEnt*>::iterator iter;
for (iter=m_ppTEntList.begin();iter!=m_ppTEntList.end();iter++)
{
(*iter)->my_entity_paint();
}
All I wanted to do was access every element of the vector and then call a function of each of the element.
Can any one point me to where I am wrong or any better way at all to how this can be done.
I am not exactly sure as to what you mean by going out of scope!!
But it seems to register it because when I test the size of the vector from the screen class I dont get a error and it says the entity has been properly registerd
I seem to have gone wrong with the pointing but I think what you said is probably the cause of the problem.
This is from a a level editor class that I use to register the entities on to the manager.
Code:
void LevelManager::InitLevel(){
//A Instance of Entitymanager to which all the instances are registered
vec3d pos;
pos.Initialize(10,0,0);
TEnt a(pos);
EntityManager* Manager=EntityManager::getPInstance();
Manager->REGISTER_ENTITY_INSTANCE(a);
}
You will have to dynamically create the TEnt in InitLevel().
If you would use a vector of smart pointers to register the entities you don't have to worry to delete the entities.
Kurt
void LevelManager::InitLevel(){
//A Instance of Entitymanager to which all the instances are registered
vec3d pos;
pos.Initialize(10,0,0);
TEnt * a = new TEnt(pos);
EntityManager* Manager=EntityManager::getPInstance();
Manager->REGISTER_ENTITY_INSTANCE(a);
// REGISTER_ENTITY_INSTANCE should take a TEnt *
}
This is another problem I have.
I got a following inheritance hierarchy.
TEnt, Male and Female inherits from a baseentity.
What I want is my Entity Manager class to access all the instances of all the three types of entities.
Currently I have a vector of pointers to each of the individual entities which i register in the register function
This would mean that I would have to declare a vector one for each of the child classes and if I increase the class count I would propotionally keep increasing. Also the register and any other function that takes in a input needs to be repeated.
Bookmarks