Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Question about inheritance and clean code

Discussion in 'Scripting' started by FreesbieCatcher, Jun 4, 2023.

  1. FreesbieCatcher

    FreesbieCatcher

    Joined:
    Jun 4, 2023
    Posts:
    1
    Hi!
    so I was told that when you have objects that behave similar, you should make a parent class take care of everything all objects share and child classes for changes to avoid spaghetti.
    I have a player character that can change forms on spot like in DMC5, some are faster, slower, more jumps, different spells. I created main class which manages all stats, movement, animation and sets up spells for different forms like this
    Code (CSharp):
    1.  
    2. public class Hero : MonoBehaviour
    3. {
    4.     protected int speedBonus;
    5.     protected int extraJumps;
    6.     protected void Start()
    7.     {
    8.        
    9.     }
    10.  
    11.  
    12.     void Update()
    13.     {
    14.         ManageMovement();
    15.         ManageAnimation();
    16.         ManageSpell();
    17.     }
    18.  
    19.     void ManageSpell()
    20.     {
    21.         if(Input.GetKeyDown(KeyCode.Q)){Spell1();}
    22.     }
    23.    
    24.     protected virtual void Spell1()
    25.     {
    26.          
    27.     }
    28. }
    29.  
    then I have a class for each form like this for example:

    Code (CSharp):
    1. public class Tank : Hero
    2. {
    3.     void Awake()
    4.     {
    5.         jumpReset = 1;
    6.         speedBonus = -2;
    7.     }
    8.  
    9.      protected override void Spell1()
    10.     {
    11.          Heal();
    12.     }
    13. }
    14.  
    Is this the right way to do it? How should I arrange this in the inspector? For now, I have deactivated Hero script, and all child scripts with a system that lets me activate one and deactivate others on my gameObject. My main concern is the Awake() and overall structure.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Inheritance... yuck.

    Interfaces... yay!

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.
     
  3. Elhimp

    Elhimp

    Joined:
    Jan 6, 2013
    Posts:
    75
    If all you want is change couple of variables, then change couple of variables. Like
    EngageTankMode()
    method. And that's it. You're overengineering.
    If actions object can perform should be changed substantially, then there https://en.wikipedia.org/wiki/Command_pattern
    Interfaces help homogenise different types to works with 'em in one bunch, create extensions, and to hide inner workings and/or other parts of functionality.
     
    Kurt-Dekker likes this.