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 Serinx · Nov 04, 2014 at 10:12 PM · inheritanceoopstructureabstractpolymorphism

How should I structure my unit class relationships?

I want to structure my classes in a way that makes sense and is easily adaptable when implementing new types of units etc.

I was thinking that I'd have a base Class: Unit. This class would define the abstract interface for all the Units including players and AI.

I would then have three classes that Implement/Inherit from Unit. They would be the different elemental types of the Unit.

So i'd have the following:

  • FireUnit : Unit

  • WaterUnit: Unit

  • EarthUnit: Unit

My problem is that I want players and AI units to act differently but still use the same Unit classes.

I can't seem to get my head around how this would work. Would Unit inherit from player? Or would player inherit from Unit?

Hopefully this makes sense, I'm quite new to OOP so I'm not even sure if i'm asking the right questions!

Thanks

Comment
Add comment · Show 6
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 Serinx · Nov 04, 2014 at 10:27 PM 0
Share

Really I'm just thinking how the Player Class and Enemy classes would be defined.

For example, if the player class was defined:

Player: Unit

Would I be able to set the Player = new FireUnit()?

avatar image AlwaysSunny · Nov 04, 2014 at 10:42 PM 1
Share

The best approach is probably having your Unit class delegate most of its logic to sub-modules which you can swap as needed. So, your unit's $$anonymous$$otor modules could be identical, but the Control modules would be different - AI units would get a version driven by AI logic, while the Player gets a version that accepts input. A great use-case for Interfaces.

avatar image devluz · Nov 05, 2014 at 12:41 AM 0
Share

You wouldn't be able to set Player p = new FireUnit(). This is against the rules :)

Could you tell me more about what FireUnit means in your game? Then I could give you an answer how I would do it.

In game development and especially in unity it often makes sense to use Composition/Aggregation based development rather than using a lot of inheritance.

e.g. Player and AI are very different but they might share some properties like life. Ins$$anonymous$$d of having the class Life and Player and AI inherit from it you could just give Player and AI a member variable of type life. You can still gain the same advantages of polymorphism by using an interface.

avatar image Serinx · Nov 05, 2014 at 01:32 AM 0
Share

@devluz The Unit Class would hold the basic attributes like Health, $$anonymous$$ovespeed, Name etc.

FireUnit would have different functions for its abilities e.g. Ignite; Whereas a WaterUnit would have a function "IceBeam"

Thinking about it I could just call them "Ability1", "Ability2" etc in the Unit class and then have them do different things within FireUnit and WaterUnit.

Also the takeDamage for example would be different in each childClass so a FireUnit would take increased damage from a WaterUnit.

avatar image Serinx · Nov 05, 2014 at 01:48 AM 0
Share

Just to elaborate if I wasn't clear.

I'd want the player class to be able to inherit from FireUnit, WaterUnit or EarthUnit. I'd also like the AI to be able to inherit from these classes.

$$anonymous$$aybe inheritance isn't what I should be using here?

Inheritance would look something like this... but player and ai can only inherit from one of the element unit classes. (Sorry for the crappy paint job :P)

alt text

Thanks for your input btw guys.

untitled.jpg (23.5 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Kiwasi · Nov 05, 2014 at 02:52 AM

Ditch deep inheritance (for the most part) and use components to go wide instead.

Each GameObject has the following components

  • Movement component

  • Weapon component

  • Armour component

  • Input component

Then each component type can have an inheritance structure

Movement component is the base for

  • Tracked

  • Walker

  • Flying

Weapon

  • Gun

  • Flamethrower

  • Water pistol

Input

  • Player controlled

  • AI (You can further derive for different AI behaviours)

Get the idea?

Your AI component simply requires a TypeOf(Movement) to be present. The AI interacts with the movement component, and never needs to know exactly what type of component it is dealing with.

The weapon component behaves the same if it is fired by a PlayerInput or an AgressiveAI2. Again, it doesn't care.

Comment
Add comment · Show 2 · 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 Serinx · Nov 05, 2014 at 03:37 AM 0
Share

Alright, I think I see where I went wrong.

Since the Units themselves defined their "weapon" type, I was looking at it in a different way.

If I think of their element as a component of the Unit class then that makes it a lot easier to wrap my head around!

Thanks everyone for your help! This is a great community :)

avatar image devluz · Nov 05, 2014 at 06:10 AM 0
Share

I would do it in a similar way. Good answer!

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

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Accessing interface in a derived class through the parent class 0 Answers

Abstract class question 1 Answer

How to work with a list of base class, if a variable is only found in some subclasses? 2 Answers

Derived Class Fields 3 Answers


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