Search Unity

Question How to make my own operation with my classes

Discussion in 'Scripting' started by Magnilum, Aug 20, 2021.

  1. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    241
    Hi, I have a question, I made this class:
    Code (CSharp):
    1. public class BonusMaxValue {
    2.  
    3.     [SerializeField] float _maxValue = 0;
    4.  
    5.     protected float bonus = 0;
    6.  
    7.     public float maxValue { get { return _maxValue + bonus; } }
    8.  
    9.     public Action<float> OnMaxValueBonusChanged;
    10.  
    11.     public void SetBonus(float amount)
    12.     {
    13.         bonus = amount;
    14.         OnMaxValueBonusChanged?.Invoke(amount);
    15.     }
    16.  
    17.     public void AddBonus(float amount)
    18.     {
    19.         bonus += amount;
    20.         OnMaxValueBonusChanged?.Invoke(amount);
    21.     }
    22.  
    23.     public void RemoveBonus(float amount)
    24.     {
    25.         bonus -= amount;
    26.         OnMaxValueBonusChanged?.Invoke(amount);
    27.     }
    28.  
    29.     public void AddMultiplierBonus(float multiplier)
    30.     {
    31.         float amount = (_maxValue * multiplier) - _maxValue;
    32.         bonus += amount;
    33.         OnMaxValueBonusChanged?.Invoke(amount);
    34.     }
    35.  
    36.     public  void RemoveMultiplierBonus(float multiplier)
    37.     {
    38.         float amount = (_maxValue * multiplier) - _maxValue;
    39.         bonus -= amount;
    40.         OnMaxValueBonusChanged?.Invoke(amount);
    41.     }
    42. }
    and I would like to multiply this classe by another or by a float to get the multiplication of the float and the maxValue. I saw in unity this line of code for Vector3 class:

    Code (CSharp):
    1. public static Vector3 operator *(float d, Vector3 a);
    So I think, it's possible to do it but I don't know how does it work.
     
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
  3. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    241
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    I will go with one method with two parameters one "ammount" other "multiplier" so you get rid of too many methods what do same thing.THen you dont need add end remove and in that case you just pass mulitlier 0.Or even add 3rd parameter for operation so you can have every operation in one method and change only in if statement

    Code (CSharp):
    1. public class UnityForum : MonoBehaviour
    2. {
    3.     float bonus = 0;
    4.     public enum Operations {Add,Remove,Set}
    5.  
    6.     public void ChangeBonus(Operations operation, float amount, float multiplier)
    7.     {
    8.         if(operation.Equals(Operations.Add))
    9.         {
    10.             bonus += amount;
    11.         }
    12.         if(operation.Equals(Operations.Remove))
    13.         {
    14.             bonus -= amount;
    15.         }
    16.         else if(operation.Equals(Operations.Set))
    17.         {
    18.             bonus = amount;
    19.         }
    20.         OnMaxValueBonusChanged?.Invoke(amount);
    21.     }
    22. }
     
    Last edited: Aug 21, 2021