I am building small components that will be used for a RPG later on, however my experience in this scale of project is very limited, which is why I choose to build them independant of each other. This part will cover character creation and handling.
So I have the following vector that stores class pointers of Gender. I know there will only be four genders, Unknown (for error-catching), Male, Female and Neuter.
std::vector<Gender*> mGenders;
However for some other vectors I do not know the exact amount.
std::vector<BClass*> mBClasses;
I have Creatures that have a mGenderID as well as a mBClassID. I have a game.h that initializes all genders and base classes (among many others). Let's say I have a
Creature* mPC = new Creature ("Name", GenderID, BClassID);
Should I then have a
Gender* Game::getGenderByID(int id) {
for (std::vector<Gender*>::iterator it = mGenders.begin(); it != mGenders.end(); ++i) {
if ((*it)->getID() == id) return (*it)
}
}
And use it the following way
std::cout << "Your name is " << mPC->getName() << ". Your gender is " << getCreatureByID(mPC->getGenderID())->getName();
So my question is, is this a good way of structuring the code? Imagine an infinite-engine like game, Baldur's Gate or so. Preferebly it should work for most, even if I were to do a Skyrim or GTA-like game.
Lastly, how easy would this be to port for C#? As I know C++ better than C#, I thought I'd give it a go in C++ first, to get the structure correct and work with something that can be used in larger-scale projects.
Edit: Would it be better to directly store the Gender* in the Creature class? And no the ID of the creature?
Edit2:
class Gender {
public:
Gender::Gender(int id, std::string name, std::string desc, int coeBonus, int strBonus, int agiBonus, int attBonus, int intBonus, int chaBonus);
~Gender();
int getID () const;
std::string getName () const;
std::string getDesc () const;
int getStrBonus () const;
int getAgiBonus () const;
int getAttBonus () const;
int getIntBonus () const;
int getChaBonus () const;
int getCoeBonus () const;
private:
int mID;
std::string mName;
std::string mDesc;
int mStrBonus;
int mAgiBonus;
int mAttBonus;
int mIntBonus;
int mChaBonus;
int mCoeBonus;
};
The definition of gender.h
Thanks in advance
I can't see a good reason to be storing pointers in your vector. Why not just have a
std::vector<Gender>
?Anyway, it seems like a strange idea to have a container of
Gender
s. As you say, you have 4 possible genders, which you are then storing in this vector. So the vector is always size 4 and you never remove anything from it or add anything to it? And each element basically just represents a possible gender that a creature can have? That is, you're enumerating the possible genders. Sounds like you want anenum
(orenum class
) instead!Then there's no need to mess around with ugly IDs that don't really mean anything (at least, they're hidden behind the enum now).
Then you could create a creature like so:
If you then want to print these out, you'll need some kind of mapping from enum value to strings. One way to do this is to just have a function that
switch
es on thegender
argument and returns an appropriate string. Alternatively, you can create astd::map
like so:Then you can print a particular creatures gender just by doing:
You seem to have an unhealthy tendency to want to use dynamic allocation for your objects. Don't do it unless it's necessary, and when it is necessary, prefer smart pointers over raw pointers.