Question regarding making classes that accept a variable...

Started by
2 comments, last by neurokaotix 21 years, 1 month ago
Currently, in my engine when you want to create a new light object for instance you would type: this->Engine->sceneManager->sceneNode->nodeLight->createLight("Light1"); This is kinda long but what my parter in the project suggested we do is something along the lines of this: this->Engine->sceneManager("Level1")->createLight("Light1"); Now, how do I make it so I can pass sceneManager the name of the level and then be able to select which function I want? I really have no idea... Any help would be greatly appreciated :D Join the World Wide Revolution:
Advertisement
You could make it like this:

  class Engine {  Engine() {    m_sceneMap["Level1"] = new SceneManager(plahplahplah);    m_sceneMap["Level2"] = new SceneManager(blehblehbleh);  }    ~Engine() {    //TODO: release mem yo lazy bum  }  SceneManager* getScene(const std::string& sceneName) {    assert(m_sceneMap.find(sceneName) != m_sceneMap.end());    return m_sceneMap[sceneName];  }  std::map<std::string, SceneManager*> m_sceneMap;};//now you can do:Engine e;e->getScene("Level1")->sceneNode->nodeLight->createLight("Light1");  

So just make a function that returns a pointer to some other object and use a map to look-up the correct object (or preferably hash_map if you have one)
Wait a sec I think I kinda misunderstood the question..

But anyway. If you want to call
getScene("Level1")->xxx;
where xxx can be just about anything, you have no choice but to make a new class that indirects the function calls to the real functions (such as sceneNode->nodeLight->createLight()), and then getScene returns an instance of this auxiliary class.
maybe something like this..

Engine->AddLight("Level","Light1";

Engine::AddLight(...)
{
// do stuff here
sceneManager->createLight(...) // or what ever..
}

So you hide as much as possible of the stuff that
is goining on.

<Oze>

This topic is closed to new replies.

Advertisement