Chose which variables to add ints to

I have a lot of stats in my game and I want to add a shop menu with buttons so i can add an amount to a stat. however, i dont want to add a seperate add method for every single stat because there are so many(health, defense, crit chance, crit damage, speed, stamina, etc).

how do i do this?

i want it to be like, you input a string and that string corresponds to a variable. then, it adds to that variable
hopefully this makes sense
help!

You could look into storing your stats as a Dictionary. For example:

Dictionary<string, int> stats = new Dictionary<string, int>();

// to retrieve a stat value:
int GetStatValue(string statName) {
  if (stats.TryGetValue(statName, out int val)) {
    return val;
  }

  return 0;
}

void SetStatValue(string statName, int newValue) {
  stats[statName] = newValue;
}

void AddToStat(string statName, int amountToAdd) {
  var currentValue = GetStatValue(statName);
  int newValue = currentValue + amountToAdd;
  SetStatValue(newValue );
}

While you’re at it you might consider using an Enum for your stats, e.g.

enum Stat {
  Strength,
  Agility,
  Intelligence,
  etc...
}

Then you could use that enum type instead of String everywhere in my above example.

1 Like

i tried adding it, it says there is no argument given that corresponds to the required formal parameter “newValue” of “PlayerStats.SetStatValue(string, int)”

How are you calling the SetStats?

exactly the way praetorblue wrote it ;-;

also how do i implement this into a ui button? @PraetorBlue

Well, you are missing a parameter then, try it this way: SetStatValue(statName, newValue);

And you can do that by dragging the script to the OnClick() event in your button and pass the amount or the stat name parameter, because I think you can only pass 1, so you’ll need to thinker a way to make it more dynamic

how do i do this? im not that good with scripting

You would need to specially craft a system for your game, you should start by learning some C# basics. Check out BRACKEYS channel in Youtube

ok thanks