Jump to content
  • Advertisement
Sign in to follow this  
Oni Sephiroth

Instantiate a class based on some file data[C++]

This topic is 3003 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

I'm having a little problem coming up with a decent algorithm for instantiating a class based on data read from a file. Basically, I have 2 files, I have entity data which defines some properties, and then there's another file that tells which of these classes should be instantiated. The former file is a custom script that looks like this...

ent_0 player CBasePlayer
{

=Events:
ev_kaboom
=End_Events

=Sprite:
mapResources/sprites/player.png

=Params:
file Sound
int Apples
=End_Params

}




The most significant part is the first line, where ent_0 gives the entity ID, player gives the entity name and CBasePlayer is the class in the C++ code that this entity is associated with. When I for example, see an ID of 0 in the second file, I want to instantiate the CBasePlayer class. When I see another number, lets say 1, I want to instantiate whatever class is associated with the number 1. I've thought about using a dictionary to map the IDs to classes, but I'm not sure how to map the classes in there. I've thought about using void pointers or templates, but I'm not really sure of a good way to implement either.

Share this post


Link to post
Share on other sites
Advertisement
Unfortunately C++ doesn't really have a hugely elegant way to do this. The solution I personally tend to settle on is to use macros:

#define FACTORYMAP MAPENTRY(FooClass) MAPENTRY(BarClass) ... // etc.

Then in code someplace:

BaseClass* FactoryMethod(const std::string& classname)
{
#define MAPENTRY(x) if(classname == #x) return new x;
FACTORYMAP
#undef MAPENTRY
throw std::exception("Invalid class name passed to factory method");
}

Share this post


Link to post
Share on other sites
Generally I resort to some kind of polymorphic factory class when I want to do something like that:

class entity_base { ... };

class entity_factory_base
{
virtual entity_base* generate() const = 0;
}

template<typename TObj>
class entity_factory : public entity_factory_base
{
// Obviously, TObj must derive from entity_base
virtual entity_base* generate() const { return new TObj; }
}

map<string, entity_factory_base*> FactoryMap;

...

class CBasePlayer : public entity_base { ... };

FactoryMap["CBasePlayer"] = new entity_factory<CBasePlayer>;

...

// Now the following will create a new CBasePlayer...
FactoryMap["CBasePlayer"]->generate()



Anyway, that's the general idea. That was just off the top of my head though, so it's only supposed to illustrate the point, I make no guarantees that I didn't do something stupid in there :)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!