• Advertisement
Sign in to follow this  

help with class design/storing my objects

This topic is 4473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I could use some help with how to design my classes in regards to storage of all created objects. Lets say that I have typical classes names Terrain, Unit, Hex etc, but how to keep track of my Terrain objects in order for easy access? I guess that one obvious way would be to store them all in terrain vector, i.e. vector<Terrain*>, outside the class implementation. However, would it work having it as a part of the Terrain class according to my feeble try below? And would it be good or bad design? Other ways of accomplish same objective?
class Terrain
{
private:
    TCHAR                     m_Name[20];
    Bitmap*                   m_pBitmap;
    static vector<Terrain*>  m_TerrainList;

public:
    // some code
    LPTSTR GetName() { return m_Name; }
    vector<Terrain> GetTerrainList( ) { return m_TerrainList; }
}
//
// the constructor
Terrain::Terrain( some variables)
{
    // do stuff with variables

    TerrainList.push_back(this);
}
//
// example use:
    vector<Terrain*>::iterator iter;
    for (iter = g_pTerrainObject->GetTerrainList().begin() ; iter != g_pTerrainObject->GetTerrainList().end() ; iter++)
    {
        // do something with (*iter)->Getname();
    }
regards /twinkle ;)

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure if this is relevant, but I think the best way of storing all data in the same area/class.

So you can easily swap in and out other components eg different type of renderer or sound etc without effecting the game data.

Share this post


Link to post
Share on other sites
So a better design would be to have another class named Data or World which contain all storage vectors etc. Guess it would facilitate game loading and saving as well. However should general methods like DrawHexes, DrawUnits etc. be in that class as well or are they better of other classes.

Regards,
/twinkle ;)

Share this post


Link to post
Share on other sites
You can have as many data classes as you want, there are many ways of storing data aswell, eg databases etc. But I only use one data class.

The draw functions etc would be in the other classes, so you might have a Particle class, which draws data some way eg sparks, and you might decide you want to completely scrap that particle class.

So if you did this, it wouldn't break the game in anyway, as no game data is stored in the Particle drawing class.

Then you can just fully design the game data structure, and have a very simple game, and then just add further rendering classes or whatever to advance the game eg sound effects, drawing models, sprites from the data etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement