Killcount stored in dictionary via custom Class, almost there but ew... need help :)

Hi there.

I’m trying to store data stuff such as kill count, friendly kills, enemy kills etc in a dictionary. I’ve it working all ready. I store the variables in a class, and then I store the class in a dictionary using as key the player name.

The setup works for a single player, but when I store also the enemy scores so that I can later on shown in an score board, or for multiplayer, it stores the same values for all players. Ok I got it, all keys do have the exact instance of the class, but I dont know how to create a new one for each key, is it possible?.

This is my stats storing script. with the class "killStatsValues " where I store the variables I later on store in the dictionary. This class is the one that I need to be unique for each key.

Its the first time I create a subclass and a dictionary so probably I’m conceptuallly doing something wrong.

Some non specific stuff to the topic ripped to make it more clear.

ublic class Stats : MonoBehaviour {
   
  public class killStatsValues {
        public int friendly_Kills;
        public int enemyKills;
    }

    private Dictionary < string, killStatsValues >  killStatsDic = new Dictionary<string, killStatsValues >();
    
    public  killStatsValues playerKillStatValues = new killStatsValues();

    public void addKill(string _Killer_Player, string _Killed_Player,bool _IsFriend, float _KillEnergy )
    {
        if (_IsFriend){    playerKillStatValues.friendly_Kills    +=1 ;   }
        if (!_IsFriend){ playerKillStatValues.enemyKills +=1 ; }

        if (killStatsDic.ContainsKey(_Killer_Player))
        {killStatsDic[_Killer_Player] = playerKillStatValues;        }
        else {killStatsDic.Add ( _Killer_Player, playerKillStatValues);    }
    }

    }

}

Please help

I think I’m missing a constructor. I’ll test how it goes and post if it works.

rewrote it SEEMS LIKE ITS WORKING! Tomorrow I will test in depth as its 2 am here and tag as solved. :slight_smile:

Serializing dictionary for saving into a file is gonna be fun… fun in the bad sense of course :slight_smile:

public class Stats : MonoBehaviour {
 
    public static Stats _stats;

    //esta class la usamos para meter varias variables en el diccionario
    public class killValues {

        //counter de kills
        public int friendly_Kills;
        public int enemyKills;

        public  killValues  (int _Fkills , int _Ekills)
        {
            friendly_Kills = _Fkills;
            enemyKills = _Ekills;
        }
    }


    //el diccionario para las killstats
    private Dictionary < string, killValues >  killStatsDic = new Dictionary<string, killValues >();
    // instancia que meteremos en el diccionario con add o updateando si ya existe la key.


    // Use this for initialization
    void Awake () {
        _stats = this;

    }

    void Update(){
        if (Input.GetKeyDown (KeyCode.R)){ Read ();}
    }

    //añadir stats para daños

    //con el prefijo player en las classes y diccionario  propios de este script se refiere a cualquier jugador no a mi jugador
    //Kill energy es el hit energy en caso de muerte,

    public void addKill(string _Killer_Player, string _Killed_Player,bool _IsFriend, float _KillEnergy )
    {
        killValues val;
        //if (killStatsDic.ContainsKey(_Killer_Player))
        if(killStatsDic.TryGetValue (_Killer_Player, out val ))     
        {
            if (_IsFriend){
                killValues values = new killValues(val.friendly_Kills +1, val.enemyKills);
                killStatsDic[_Killer_Player] = values;
            }
            if (!_IsFriend){
                killValues values = new killValues(val.friendly_Kills, val.enemyKills + 1);
            }
            /*
            killStatsDic[_Killer_Player] = killValues; 
            */
        }

        //for the first time values are stored for this key.
        else {     
            if (_IsFriend){  
                killValues values = new killValues(1, 0);
                killStatsDic.Add ( _Killer_Player, values);
                killStatsDic[_Killer_Player] = values;

            }
            if (!_IsFriend){
                killValues values = new killValues(0, 1);
                killStatsDic[_Killer_Player] = values;

            }
        }

edit. tested a bit and seems to work partially, the first time the data is stored ok with independent values for each player but it seems to not work when adding new values… I leave this for tomorrow.