Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualfastcall22

Posted 05 November 2012 - 12:08 PM

I know I shouldn't use singletons a lot. How can I avoid this?

You can use a factory, then pass the factory around:

class Factory {
private:
    map<string,Entity*> prototypes;

public:

    Entity* create(const string&amp; 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&amp; c, Factory&amp; 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..

#1fastcall22

Posted 05 November 2012 - 12:07 PM

I know I shouldn't use singletons a lot. How can I avoid this?

You can use a factory, then pass the factory around:

class Factory {
private:
    map<string,Entity*> prototypes;

public:

    Entity* create(const string& name) {
        if ( prototypes[name] )
            return prototypes[name];
    }
};

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..

PARTNERS