- Home /
Problem using variable of type CustomStat to indicate which player statistic to change
I'm trying to create a single, highly modular Buff class that anything which wants to alter player stats can call to alter any stat. I have a list of type UtilityStat called statList, UtilityStat being a custom stat class that contains a bunch of floats: health, MP, and so forth. If I want a method to create a debuff that slowly reduces HP, I want to go:
UtilityStat targetStat = UtilityStat.HP; //This is what we're reducing
Buff myBuff = new Buff(); //Create a new, empty buff that does nothing
myBuff.AddToStatList(targetStat, -1f); //Add a new effect to myBuff, "Add -1 to targetStat every tick"
To do this, I added the following in my root Buff class:
public List<UtilityStat> statList; //List of stats to be altered
public List<float> statChange; //Amount to alter each stat by
public void AddToStatList(UtilityStat stat, float change){//External functions run this to add values to statList & statChange
statList.Add (stat);
statChange.Add (change);
}
public override void UpdateEffect (Character character){
for (int i = 0; i < statList.Count; i++) {
character.utilityStat.statList[i] += Mathf.Clamp(statChange[i],0f,100f); //Iterate through statList, and for each index, find the matching utility stat on the player and increment it by statChange[i]
}
}
However this returns an error on the line that starts character.utilityStat.statlist[i], namely "Type 'UtilityStat' does not contain a definition for 'statList' and no extension method 'statList' of type 'UtilityStat' could be found". Is what I'm trying to do possible, or would I need to create multiple buff classes that each specifically target a single value in the player's UtilityStat list?
Answer by InvincibleCat · Jan 22, 2015 at 02:08 AM
because the error on
character.utilityStat.statList[i]
is that statList is not a member of your class UtilityStat
And
statList[i]
Exists but is not a float
Maybe you should have your UtiltyStat class like:
public class UtilityStat
{
public float Value = 0.0f;
}
And then:
statList[i].Value += statChange[i];
Also,
Mathf.Clamp(statChange[i],0f,100f);
Doesn't allow negative value. So
myBuff.AddToStatList(targetStat, -1f);
won't do anything
Yea, character.UtilityStat is a single UtilityStat, which is a class that contains six floats (HP, $$anonymous$$P, etc), so I was trying to target a specific value in character.UtilityStat, let's say HP, by saying statList[0] = UtilityStat.HP.
And if Clamp doesn't take negative values, would I just want to run the calculation on two lines?
Character.UtilityStat.Whatever += statChange[i];
Character.UtilityStat.Whatever = $$anonymous$$athf.Clamp(
Character.UtilityStat.Whatever, 0f, 100f);
And if Clamp doesn't take negative values, would I just want to run the calculation on two lines?
Yes this is better.
Can you share more of your code ?
Sure, what do you want to see? The utilitystat class itself just reads
[Serializable]
public class UtilityStat{
public float HP;
public float $$anonymous$$P;
}
and the character class where the list I'm trying to access is just a data holder:
public class Character{
string name;
UtilityStat utilityStat;
Animator animator;
List<StatusEffects>() effectsList;
}
Ok so that makes sense. You need a public accessor to the utilityStat variable !
Public accessor as in setting utilityStat to public? That results in the same error...
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
FPS weapons class structure 1 Answer
Calling a method of a class that is a part of another class? 1 Answer
HideInInspector with inherited variables 1 Answer
Cram variables inside class, use inheritance, or something else? 1 Answer