Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MortalWombat922 · Jan 16, 2015 at 08:40 PM · instantiateinheritanceorganization

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:

  1. BaseCharacter inherits from BaseAttribute

  2. BaseAttribute inherits from MonoBehaviour

  3. CharacterPhysicalAttributes inherits from MonoBehaviour

  4. PhysicalAttributeModifiers inherits from CharacterPhysicalAttributs

  5. CharacterLowLevelSkills inherits from MonoBehaviour

  6. LowLevelSkillModifiers inherits from CharacterLowLevelSkills

  7. CharacterHighLevelSkills inherits from MonoBehaviour

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MortalWombat922 · Jan 17, 2015 at 12:58 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges