View Single Post

Thread: Who wants to help me with some code?

  1. - Top - End - #2
    Orc in the Playground
     
    ElfRangerGuy

    Join Date
    Dec 2012
    Location
    Island of tea
    Gender
    Male

    Default Re: Who wants to help me with some code?

    That looks like a rather odd way to code it - IIUC XSword will have the same functions and variables as YAxe and all other Weapon subclasses?

    Also, what creates new Weapon [subclass] objects and how? In particular, how does whatever it is know what subclasses of Weapon exist?

    I'd have a struct WeaponType containing variables for each of your stats, a function to create instances of it from data in a file, and have the Weapon constructor take a WeaponType pointer as an argument. That way you can avoid storing numerous copies of the same data.

    Code:
    #ifndef WEAPONSTUFF_H
    #define WEAPONSTUFF_H
    
    struct WeaponType {
      string _name;
      int _dur;
      char _rank;
      int _rng;
      [etc]
    }
    
    vector<WeaponType*> loadTypes([filename/pointer]) {
      [do stuff I'm to lazy to care about just now - Dr Who is on]
    }
    
    class Weapon : public Item {
    public:
      Weapon(WeaponType *in_type) { _type = in_type; } //const? Probably.
      int rng() const { return _type->_rng; }
      [etc]
    private:
      WeaponType* _type;
      [etc]
    }
    #endif
    Then you have a vector (or something else) containing the stats for every type of weapon, you can look at it to see what types of weapon can be created, and you can easily add new weapon types by adding them to a file without recompiling the program.

    I'm only a hobbyist programmer though, so please do correct me if I'm being a total idiot somehow.
    Last edited by FLHerne; 2013-11-23 at 03:02 PM.