You can use a factory, then pass the factory around:I know I shouldn't use singletons a lot. How can I avoid this?
class Factory {
private:
map<string,Entity*> prototypes;
public:
Entity* create(const string& name) {
if ( prototypes[name] )
return prototypes[name];
}
// Or:
// template<class T>
// T* create() { }
// template<> Fireball* create() { return (Fireball*)prototypes["fireball"] /*or etc...*/; }
};
struct Game {
Factory f;
};
void test( Character& c, Factory& f ) {
c.doSkill(f.create("fireball"));
// or: f.create<Fireball>()
}
Or etc...You can also use const char* over std::string if you need to avoid frequent memory allocations there..