e_barroga:
Please read the posts more carefully.
Using an object manager class, your code could look like as follows:
int main() {
ObjectManager mgr;
mgr.addObject( new MyObject ); // MyObject derived from BaseObject
mgr.addObject( new MyOtherObject ); // MyOtherObject derived from BaseObject
while( true ) { // Some kind of main loop.
mgr.processObjects(); // Calls step() for every object.
}
}
BaseObject has the virtual functions create() and step(). create() can be called in several ways: Either by the constructor or by ObjectManager::addObject(). ObjectManager::processObjects() iterates through all added objects and calls step() on each. I hope it's more clear this time.
Edit:
klusark:
You're implementing some kind of factory pattern. I would either prefer to implement functions like: createPlayer(), create...() instead of comparing type strings (we're using OOP ), or directly pass a pointer to the proper object by using the new statement.
For example something like:
objectmanager.register( ObjectFactory::createPlayer(), "an_unique_id" );
Yes, I already know about writing an object manager, but I am having issues writing the method to create instances through arguments.
void ObjectManager::addObject ( argument0 ) {
new argument0;
}
How do I make it create an instance of whatever is defined in argument0?
Oh I see, my old post got what you wanted wrong.
You seem to want to create an object instanced to a name, have a look at std::map
-Explination will be here soon-
Basically, the first thing you want after you've got your base class is an object manager, you want your objects to have names i'm assuming so you do something like this
class ObjectManager
{
private:
std::map<std::string, BaseClass*> tObjects; //We are making an associative array here, in other words the string will refer to the object
public:
void addObject(const std::string& name, BaseClass* data);
void step();
}
void ObjectManager::addObject(const std::string& name, BaseClass* data)
{
tObjects[name] = data; //Think of it sorta like a dynamic array that instead of using ints, uses strings, so we set the "Name" position to the "Data"
}
void ObjectManager::step()
{
for(std::map<std::string, BaseClass*>::iterator mIter = tObjects.begin(); mIter != tObjects.end(); mIter++)
{
mIter->second->step();
}
}
Maybe that is what you are looking for
Another example is the first class I made in SFML, I call it the resource manager, it simple handles Image data for me so that there is never more then one of the same image, while it doesn't use a Base class, it shows you how I associate a std::string to another object. Allowing you to name it as I believe you want to.
/// ---------- ResourceManager.hpp -----------
class ResourceManager
{
private:
static Logger* Log;
std::map<std::string, sf::Image*> Images;
public:
~ResourceManager();
static void setLogger(Logger* a) {Log = a;}
//Images
void loadImage(const std::string& name, const std::string& filepath);
const sf::Image& getImage(const std::string& name);
};
///-------------------------------------------------
///------------ ResourceManager.cpp ----------
Logger* ResourceManager::Log = 0;
ResourceManager::~ResourceManager()
{
for(std::map<std::string, sf::Image*>::iterator mIter = Images.begin(); mIter != Images.end(); mIter++)
{
delete mIter->second;
}
}
void ResourceManager::loadImage(const std::string& name, const std::string& filepath)
{
// Test if the key exists
if(Images.find(name) == Images.end())
{
Images[name] = new sf::Image();
if(!Images[name]->LoadFromFile(filepath))
Log->log("Error Loading Image: " + name + " at filepath: " + filepath, LOGTYPE_ERROR);
else
Log->log("Loaded image " + name, LOGTYPE_EVENT);
Images[name]->CreateMaskFromColor(sf::Color(255, 255, 255), 0);
}
else
{
Log->log("Attempt to re-load image " + name + " aborting load", LOGTYPE_ERROR);
}
const sf::Image& ResourceManager::getImage(const std::string& name)
{
// Test if the key exists
if(Images.find(name) == Images.end())
{
Log->log("Image " + name + " does not exist, a blank image will be returned", LOGTYPE_ERROR);
}
return *Images[name];
}
}
///-------------------------------------------------