Search Unity

Multi-level inheritence (Yuk!), how can I implement my design? [NVM, I'm an idiot...]

Discussion in 'Scripting' started by Tset_Tsyung, Jun 14, 2018.

  1. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Hey all,

    Okay, so this is probably more about design than the coding... but I would still love your feedback.

    Here's the summary of the issue.
    • Each player can have differnet loadouts for their drone.
    • Some are weapons, some are tools.
    • Some weapons are physics based, some are field based, some are raycast/energy weapon based. Tool are broken up similarly.
    So, what I was thinking of was having a "GearBaseClass" that simply has 2 abstract functions: TriggerDown and TriggerUp.

    I then SubBaseClasses which inherit from GearBaseClass for the different weapons (physics, field, energy) and some for the different types of tools.

    I will then have subClasses for each specific weapon/tool type. This will inherit further the TriggerDown() and TriggerUp() functions.

    This works! XD (Yay, for me!)

    However, to reduce the amount of typing and copy/pasting that I do I was going I was hoping to write some functionality into the SubBaseClasses - things like public transform fields for spawn points, public floats for velocity, etc.

    But you can't inherit fields.

    Okay, so I try to set up some abstract properties instead as in the following code:
    Code (CSharp):
    1. public class GearPhysicsWeaponBaseClass : GearBaseClass
    2. {
    3.     public abstract Transform SpawnPos { get; }             // pretty sure these wouldn't work anyway.
    4.     public abstract GameObject RoundPrefab { get; set; }    // pretty sure these wouldn't work anyway.
    5.     public abstract float MuzzleVelocity { get; set; }
    6.     public abstract float ReloadTime { get; set; }
    7.     [SerializeField]
    8.     private float reloadTime;
    9.  
    10.     private float nextReload;
    11.  
    12.     public override void TriggerDown()
    13.     {
    14.     }
    15.  
    16.     public override void TriggerUp()
    17.     {
    18.     }
    19. }
    Problem is that the 2 floats (as well as the transform and gameObjects) give me the error
    The thing is that I don't want to do anything with it here in the SubBaseClass - just declare it. Can I not do that?
     
  2. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    P.S. I'm also not going to be doing anything in the TriggerDown() and TriggerUp() functions, as this is to be handled by the individual weapons and tool themselve - I'm emplementing an override here just to stop another error o_O

    Needless to say, inheritence is stll very new to me...
     
  3. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    [sigh]

    Wait... nevermind... in the name of being thorough I made this classe abstract true, and that sorted it.

    I mistakenly thought I'd seen it written somewhere that an abstract class couldn't inherit from another abstract class...

    Well don't I feel like a muppet, lol.