Search Unity

Can I do this in C#?

Discussion in 'Scripting' started by supermoof, Jun 14, 2019.

  1. supermoof

    supermoof

    Joined:
    Sep 24, 2015
    Posts:
    45
    Hey there, I have a question to ask. Here is sample code for a base class called: StatAttribute and another class that inherits from it.

    Code (CSharp):
    1. public abstract class StatAttribute
    2. {
    3.     public StatAttribute(GameObject character)
    4.     {
    5.         this.character = character;
    6.     }
    7.  
    8.     protected GameObject character;
    9.  
    10.     protected int baseValue;
    11.  
    12.     public int BaseValue { get => baseValue; set => baseValue = value; }
    13.     public abstract int BonusValue();
    14.  
    15.     public int FinalValue()
    16.     {
    17.         return BaseValue + BonusValue();
    18.     }
    19. }
    20.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Linq;
    3.  
    4. public class Strength : StatAttribute
    5. {
    6.     public Strength(GameObject character) : base(character)
    7.     {
    8.     }
    9.  
    10.     public override int BonusValue()
    11.     {
    12.         IStrengthBonus[] bonuses = character.GetComponents<IStrengthBonus>();
    13.  
    14.         if(bonuses.Length > 0)
    15.         {
    16.             return bonuses.Sum(bonus => bonus.GetBonus());
    17.         }
    18.         else
    19.         {
    20.             return 0;
    21.         }
    22.     }
    23. }
    24.  
    Question: Is it possible to pass in an "interface" object/type?? (ex: IStrengthBonus) into the constructor of StatAttribute so I can move the BonusValue() function to the base class itself?
     
    Last edited: Jun 14, 2019
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,947
    Maybe something like this? You can use generics, as long as it fits GetComponent constraints.
    Code (CSharp):
    1. public interface ISomeInterface {
    2.     }
    3.  
    4.     public abstract class BaseClass {
    5.         public abstract void DoSomething<T>() where T : MonoBehaviour; // Or any other constraint, like IStrengthBonus
    6.     }
    7.  
    8.     public class Inheritor :  BaseClass {
    9.         public override void DoSomething<T>() {
    10.             GameObject go = new GameObject(); // For testing purposes only
    11.             T[] components = go.GetComponents<T>();
    12.         }
    13.     }
    Also, calling classes XAttribute might not be a good idea.
    That naming usually used by C# attributes, e.g. it will be suggested as [Stat].
     
    supermoof likes this.
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,290
    Make your base class generic and pass that interface as a type argument:

    Code (CSharp):
    1. public class Stat<T> {
    2. }
    3.  
    4. public class Strength : Stat<IStrengthBonus> {
    5. }
     
    supermoof and xVergilx like this.