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

having a class for Stats and do calculs with it.

Discussion in 'Scripting' started by Tiny-Tree, Aug 29, 2014.

  1. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,314
    Hello, this is probably a simple things but i would have advice how to make my code more efficient and cleaner.

    i currently use a class to quickly manage my data:
    Code (CSharp):
    1. public class Stats
    2. {
    3.     public float health{get; set;}
    4.     public float healthRegen{get; set;}
    5.     public float energy{get; set;}
    6.     public float energyRegen{get; set;}
    7.     public float armor{get; set;}
    8.     public float evasion{get; set;}
    9.     public float movement{get; set;}
    10.     public float jump{get; set;}
    11. }
    Then i have an Entity script that will manage the calcul of stats for example:
    i want final stats = base + skill + item
    Setter is an inspector script that return a stats structure
    Code (CSharp):
    1. public class Entity : MonoBehaviour {
    2.     #region variables
    3.     private Transform _transfom;
    4.     public StatsInspectorSetter BaseSetter;
    5.     public StatsInspectorSetter PassiveSetter;
    6.     public StatsInspectorSetter ItemSetter;
    7.     EntityStats entityStats;
    8.     #endregion
    9.  
    10.  
    11.     void Start()
    12.     {
    13.    
    14.         InitializeStructure ();
    15.     }
    16.  
    17.  
    18.  
    19.  
    20.  
    21.     private void InitializeStructure()
    22.     {
    23.         entityStats = new EntityStats();
    24.         entityStats.Base = BaseSetter.Setter;
    25.  
    26.  
    27.         if(PassiveSetter != null)
    28.         {
    29.             entityStats.Passive = PassiveSetter.Setter;
    30.    
    31.         }
    32.            
    33.         if(ItemSetter != null)
    34.             entityStats.Items = ItemSetter.Setter;
    35.  
    36.     }
    37. }
    38.  
    how could i simply do
    entityStats._Stats = entityStats.Base + entityStats.Passive ?

    i currently thought about this, but i feel there is a better way:

    Code (CSharp):
    1. public Stats AddStatsCalcul(Stats value1, Stats value2)
    2.     {
    3.         Stats calcul = new Stats();
    4.         calcul.armor = value1.armor + value2.armor;
    5.         calcul.energy = value1.energy + value2.energy;
    6.         calcul.energyRegen = value1.energyRegen + value2.energyRegen;
    7.         calcul.evasion = value1.evasion + value2.evasion;
    8.         calcul.health = value1.health + value2.health;
    9.         calcul.healthRegen = value1.healthRegen + value2.healthRegen;
    10.         calcul.jump = value1.jump + value2.jump;
    11.         calcul.movement = value1.movement + value2.movement;
    12.         return calcul;
    13.     }
     
  2. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    Everytime you call the AddStatsCalcul method you're going to be creating a new Stats object, which is a waste when you may only be using a handful of stats at any given time. Why not simply add the floats when you need them and save that extra allocation?
     
  3. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,314
    yes the example only have a few amount of stats, but the game will have more. so my point is to find a method to manage stats more easily, if i add stats later for example. so there is no way to do stats3 = stats1 + stats2?
     
  4. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    What I was attempting to get across in the last post, was regardless of the amount of stats that you have or don't have you're creating a new class each time you calculate the stats creating wasted resources.

    If each of your stats (base, passive, etc.) are always going to be in the same order, you could group everything in an array and do something akin to the following.

    Code (csharp):
    1.  
    2. /*
    3. 0 - health
    4. 1 - health regen
    5. 2 - energy
    6. .
    7. .
    8. .
    9. */
    10.  
    11. private float RetrieveCalculatedTotal(int index)
    12. {
    13.     return (BaseStats[i] + ModiferStats[i]);
    14. }
    15.  
    If you don't want to have to keep track of indexes, you could also look into using dictionary pairs of strings and floats. There are quite a number of ways to do this, just not so simple as ClassName1 + ClassName2.
     
  5. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Create in Stats class Overloadable Operators (C# Programming Guide).

    For example:

    Code (CSharp):
    1. public static Complex operator +(Complex c1, Complex c2) { /*more code here; return complexInstance;*/ }
    You can write:

    Code (CSharp):
    1. Complex complexSum = complexInstance1 + complexIntance2;
     
  6. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    @IsGreen Operators had come to mind when typing my responses, but I was worried with how often the various stats would be updated this would create a lot of garbage by constantly creating and returning new instances of the stat class that held the results. If I am mistaken in how this works please let me know, I have not worked with operators with much frequency.
     
  7. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,314
    ah this is probable something i would use.

    actually i declare the class in Start() only once, then i calcul final stats with buff/debuff once every 20 secondes
    base = new Stats()
    item = new Stats()
    skills = new Stats()
    _base = new Stats() // base+item+skills
    etc

    then the values are assigned by other components.

    if i understand its something like this:

    public static Stats operator+( Stats stats1, Stats stats2, Stats stats3)
    {
    // should i return ?
    //or i can directly assign my _Stats = result;
    }
     
  8. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Yes, it is. But do not ask me about memory management.

    Other objects in Unity as Vector3 or Rect need to create new instances each time they are modified.

    I have just answered the @Damien Delmarle question: so there is no way to do stats3 = stats1 + stats2?