Public Group

# Polymorphism question

This topic is 4156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been reading several articles that have conflicting opinions on the use of virtual functions, and so I wanna explain my situation and see what my best choice of action is. In RPGs, character classes all act differently: stats increase at different rates, skills open up at different times (if at all), and so on. Would using this base class...
class Role {
std::string Name;
int Hit_Dice;

Role ();
virtual int Base_Attack() =0;
friend class Character;
};

...and creating separate inherited classes for fighter, mage, monk, etc. be the best solution? Is there a simpler way to do this? I'm not worried about performance issues, since I've yet to work with graphics, but I don't wanna get in a bad habit of using virtual functions where they don't belong.

##### Share on other sites
If you have a pointer of the base class type that's going to point to it's polymorphed types, then then you have no choice but to use virtual functions... unless you want Early Binding (I think that's the right definition).

##### Share on other sites
Hm, well, I'd just make a class representing all classes(hehe) or archetypes.

If yuou are going to have a substantially different classes, you could inherit classes for each, but as I can think of it, no class (archetype) would have extra or need more variables.. just the variables would be different.

Also, if you DO inherit, whats handy about it is you could do:
vector<Role> players;for(int i = 0; i < players.size(); i++){  //some stuff  players.SomeFunctionInRoleAndInheritedByOthers();  //some more stuff}

Hope that helps!

Edit: To clarify.. archetypes will have differences, but the way I do it wouldnt matter. Let me explain: A archetype class will have things like Name, stats, equipment, spells, etc.

Let's say you want Monk to have a special ability that no other one has - you'd make the spell part of the monk tree. How I do it is load spells via text files and then you can have a seperate file representing the Monk skill tree with spell names that you read and parse and thus add that skill tree to the newly created Archetype object, along with other info.

##### Share on other sites
Quote:
 Original post by Drunken_MonkeyIf you have a pointer of the base class type that's going to point to it's polymorphed types, then make the function virtual.
I think the OP is asking whether and to what extent to use polymorphism, not how to do it.

@The OP: You might take a look at this thread, and in particular SHilbert's post. The post and the links included in it seem like they might be relevant.

##### Share on other sites
That is just the advice I was looking for: overall structure is something I'm having a trouble seeing (especially as my projects begin to grow) and knowing my solution is a step closer to good structure is encouraging.

Edit: @Crazyfool - That was actually my inital solution, but I ran into some complicated problems when it came to leveling in said class (or multiple classes, in this case). I'm working with the D&D system, and each class has a progression table you can follow to let you know how the different stats change over time.

Instead of copying a table such as that and writing code to read the table from a separate file is something I don't quite understand yet, so I'm trying to use functions that will calculate the different stats when called. Since each class uses a different calculation (eg. Fighter's base attack goes up 1 every level, starting at 1, while a Wizard's base attack goes up 1 every other level, starting at 0) virtual functions seem to be a perfect choice.

##### Share on other sites
Having inherited classes is probably fine. As you seem to realize, the performance hit of virtual functions probably isn't an issue. However, while I see no problem with complex class systems when it comes to organizing code, how about trying a better system for the basic role-playing engine? Just have skills, and let people choose which ones to develop, instead of having character classes? They were a bad idea when D&D implemented them, and the only thing that's changed since then is that there are now countless examples of better ways to do things available. Why do computer games seem to all use the most primitive role-playing mechanics to be found?

##### Share on other sites
You bring up memories of an argument I got into with my very first DM :)

He hated the class system Gygax and company put into the game, for much the same reasons you do (or at least those I'm reading): they restrict gameplay from the player and entrench players in a role they may not like. It's a tough question, how much freedom to give your players, and in the game I'm doing right now (I'm currently in the character creation process, haven't even begun world mechanics yet) I've found a good common ground between the two.

If you don't wanna take a class and instead wanna customize your build, you're free to do so. The templating found in D&D is something I'm simply offering to my players, since it does offer a great variety of customizing and immersion when combined correctly. If you wanna multiclass monk/fighter, just make sure you meet the requirements and find a suitable trainer. If there is no class that fits your needs, increase the skills you want and discard the ones you don't.

I ran a very successful campaign like this several years ago, and I wanna transfer that idea to a video game...or Rogue-clone, as it were ;)

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999554
×