- Home /
Class Organization and Inheritance
Hello, I'm trying to make an RPG with a few different levels of attributes for characters. The way I have the class inheritance organized right now is like this:
BaseCharacter inherits from BaseAttribute
BaseAttribute inherits from MonoBehaviour
CharacterPhysicalAttributes inherits from MonoBehaviour
PhysicalAttributeModifiers inherits from CharacterPhysicalAttributs
CharacterLowLevelSkills inherits from MonoBehaviour
LowLevelSkillModifiers inherits from CharacterLowLevelSkills
CharacterHighLevelSkills inherits from MonoBehaviour
HighLevelSkillModifiers inherits from CharacterHighLevelSkills
What I'd like to do is instantiate a new character using the base character class. The problem is, I'd like to be able to access the variables in physical attributes, low level skills, and high level skills from base character. I know multiple inheritance isn't supported, so I can't simply make BaseAttribute inherit from all of the classes I'd like it to.
One solution I can think of is moving the contents of CharacterPhysicalAttributes, CharacterLowLevelSkills, and CharacterHighLevelSkillsinherits into BaseAttribute. The problem with that is it becomes very confusing, as it makes BaseAttribute have over 1000 lines. This is sub-optimal.
The second solution I thought of was referencing all of the scripts I'd like in base character through a game object. In this case, I don't think it will instantiate new versions of the attribute scripts for each new NPC, so this won't work at all.
The third solution I thought of was making the inheritance work in a long, straight line. So CharacterLowLevelSkills would inherit from PhysicalAttributeModifiers, for example. I feel like this could also cause problems, but I'm inexperienced enough to not understand the consequences. It seems like chaining 8 scripts together this way would cause problems somewhere.
I'd greatly appreciate it if someone could clarify this for me, or show me a source where I can read up on it. Thanks a lot!
Answer by Owen-Reynolds · Jan 17, 2015 at 12:03 AM
People have been playing with the stuff for a long time, and there are lots of implementations, all depending on lots of specific details, how you think the project might grow, and what you're good at.
This might be for the Unity Forums, since it's so subjective and discussy. Or, probably better, just a general class design forum. Seriously, an experienced OOP programmer who's never seen a game would have good advice here.
IsA/hasA is a common place to start. Many people would give the characters skill variables (the character isn't a skill, it has a skill.) The best advice I ever heard was that the purpose of inheritance is polymorphism, so where do you want polymorphism (like, an array of Critter, where the actual items might be subclasses.)
Why do you need different types for high and low level skills? Why not just mark skills with a level, then you can sort them however you like? I'm mentioning this to show just how open-ended this is.
I see. This must depend more on program$$anonymous$$g knowledge than I realized. I'm sorry if this was a bad place to ask; I am just a beginner. I'll need to read up more on polymorphism. Thanks a lot for a great response. I'll ask in a more appropriate environment if I continue to have trouble after reading more.
Your answer
Follow this Question
Related Questions
Difficulty instantiating to a custom class 2 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer
Can I call a class's method which inherit monobehaviour by a normal class? 0 Answers
The type 'myclass' does not have a visible constructor that matches the argument list 1 Answer
Different sons of inheritance or a different parameter in construction 1 Answer