Isn't there no way to generalize the class's variable name?

So, I have(need to do) unity javascript code like this,

class Stat{
 var name : String;
 var hp : int;
 var mp : int;
 var some1 : int;
 var some2 : float;
}
 
var p1stat = Stat();
 
 
function mod(name : String, much : int, turn : int){
 if(name == "hp"){
  p1stat.hp += much;
 }
 if(name == "mp"){
  p1stat.mp += much;
 }
 if(name == "some1"){
  p1stat.some1 += much;
 }
}

Is there a neat way to express those stat names to just one generalized expression?

Eventually I want shorten the processes of if(name==…).

Thanks.

I was about to write up a thing on Reflection, but then I realized you could just use a Dictionary to achieve this and it would end up being simpler in the long run. Sorry I don’t know how to write it in UnityScript, so you’ll have to do a bit of translation.

public class Stat()
{
    public Dictionary<string, int> stats = new Dictionary<string, int>();

    public Stat()
    {
        stats.Add("hp", 100);
        stats.Add("mp", 100);
    }

    public void ChangeStat(string statName, int amount)
    {
        stats[statName] += amount;  // should probably add some null checks in here to be safe
    }
}

Heya,

I code in Javascript as well, and have been using hashtables like a feind for stuff like this.

If you are unfamiliar with them, they are like Arrays, however instead of categorizing things with numbers, you can use names instead.
This would be ideal for you since you are already sending your “stat” a string to decide which value to change, instead try this:

var stats : Hashtable = new Hashtable();  //make hashtable
stats.Add("hp",1);                                  //add new elemnent
stats["hp"] += 10;                                  //access that element and + 10 to it     
print(stats["hp"]);                                  //print element (should = 11)

these are the simple aspects of Hashtables, you can see more here Unity - Scripting API: Hashtable

If you went in this rout then you wouldn’t need to make a stat object altogether since the hashtable is good for storing data like this. But you still could, just make a hashtable as a variable to the entire class and edit it with functions

heres a function editing example if you use sendMessage

function setStat(_name : String,_val : int){
      stats[_name] = _val;
}

hope it helps
-Chron

The heavy duty way of doing what you want, as stated in an earlier post, is via reflection. That said, it is a reasonably advanced topic, and if you did not arrive at the concept of using reflection to solve your particular problem then I would hesitate to use reflection in this case as your first adventures in to that territory.

That said, if you did use reflection for the above, I would also question it as an overly cumbersome solution for such a small data strcture. Again, as stated in an earlier post, a Hashtable (a special kind of dictionary) would serve your purposes a little simpler and is very easy to work with.