Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Components.. Inheritance.. I just want organized code

Discussion in 'Scripting' started by Crayz, Jul 4, 2014.

  1. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    192
    I'm still quite new to C# and Unity3d scripting, though for the past 3 days or so I've been simply re-writing the code I currently have trying to come up with the best way to keep it optimized and organized. Last night I began on something that seemed promising at first, but now I'm just lost again trying to find the next best way to keep my code clean.

    Please correct me if my terminology is off..

    Basically what I have is a parent class, Character, derived from MonoBehavior, with a helper class called CharacterData used for setting up character properties, such as movement speed, max health, etc.

    So Character : MonoBehavior calls CharacterData like so:
    Code (csharp):
    1. public CharacterData cData = new CharacterData();
    And also inside my Character class I'll have an override function set up..
    Code (csharp):
    1. public virtual void Movement() {}
    That way I can set up a child class for Player's controller functions, and a child class for Enemy with the enemy AI movement.. i.e. the Player_Movement class like this:

    Code (csharp):
    1. public class Player_Movement : Character {
    2.      virtual override void Movement() { // Movement code here }
    3. }
    This seems like a pretty solid route for keeping my code clean and organized, but after realizing I also need to keep other functions organized, such as player attack, enemy attack, player pressing use on object, etc., etc., how would I go about adding all this additional code in the cleanest way possible? Should I continue with class inheritance on my Character class?

    CharacterData contains both movement properties as well as health properties, should I be keeping these separate by separating CharacterData into MovementData & HealthData etc, then parenting separate classes like CharacterHealth -> children would be EnemyHealth & PlayerHealth.. CharacterMovement with children PlayerMovement & EnemyMovement

    There's probably a few ways of going about this, I just haven't quite put my finger on anything yet.

    If somebody could shed some light on this for me I'd appreciate it
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Yes. Just keep asking the question "Does every character can attack?" If so, the base class should have an attack method.


    "Does all the character attack in a completly different way?"

    Abstract method

    "Does all the character attack in exactly the same way?"

    Not overridable method

    "Does all the character attack in a similar manner, but some does special thing when they do?"

    Virtual method

    Is there a practical need for that or is it only for "organization"? If it's just for organization, I would say to not over design it. Form follows function.
     
  3. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    In general you want to prefer aggregation (i.e. components) rather than inheritance. Maybe MovementData should be a CharacterPhysics component and HealthData should be a Damageable component and then your Character class is more of a CharacterController component. That usually is a more flexible, long term architecture and avoids the necessity to promote even slightly shared functionality to the base most class. This is, in fact, why Unity itself uses components.

    That's not to say you should never use inheritance, but it should be pretty narrow, where the subclasses are different implementations of a very narrow base interface. Unity's Renderer and Light are good examples of how to use inheritance in a limited way within a component framework.
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    light?
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Dunno... I guess "Light" is a "Behaviour", and a "Component"... and a UnityEngine.Object.

    Unity is strongly using polymorphism for all their types.
     
    IvanAuda likes this.
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I think he meant to say that PointLight, DirectionalLight, SpotLight are subclasses of light, which they're not. But I don't want to put words in his mouth, just clarifying.
     
  7. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    192
    Hmm alright thanks for the replies, played around with my scripts a little bit & I think I have a good idea where to start now. From here it's just a slow learning curve with some trial and error.

    Thanks again :cool:
     
    Last edited: Jul 5, 2014
  8. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    Derp. Light is a bad example. Was thinking of another engine. Collider would be a 2nd example in Unity.