Search Unity

Storing Sport Statistics - Class Instance or List of Arrays

Discussion in 'Scripting' started by adentutton, Jan 2, 2017.

  1. adentutton

    adentutton

    Joined:
    Mar 24, 2013
    Posts:
    34
    Hi guys!

    Just looking for some feedback about how to best store the statistics for my sports sim. I started with simple variables for each stats but wanted to have more information attached to the when the point was scored (e.g. at what point of the match, what position was the skill performed from, what was the skill result etc). At the moment I have the below code where an array is created and assigned to the next position in a list

    Code (CSharp):
    1. void AssignStatistic(int matchID, int playerID, skillType _skillType, int _skillResult, int teamAScore, int teamBScore, int teamASets, int teamBSets, int teamACurrentRotation, int teamBCurrentRotation, int startPosition, int finishPosition, int startCoordinates, int finishCoordinates) {
    2.  
    3.         int skillType = (int)_skillType;
    4.         int skillResult = (int)_skillResult;
    5.  
    6.         object[] stats = { statID, matchID, playerID, skillType, skillResult, teamAScore, teamBScore, teamASets, teamBSets, teamACurrentRotation, teamBCurrentRotation, startPosition, finishPosition, startCoordinates, finishCoordinates };
    7.  
    8.         statistics.Add(stats);
    9.  
    10.         commentary = ("Added" + statistics[statID][2] + "\n" + commentary);
    11.         commentarybox.text = commentary;
    12.  
    13.         statID++;
    14.  
    15.     }
    16.  
    17.     public void testAddStat (){
    18.  
    19.         AssignStatistic(2, 2, skillType.Set, 2, 2, 2, 0, 0, 1, 2, 2, 2, 2, 2);
    20.  
    21.     }
    22.  
    This code works fine for me but the next part of displaying the statistics is counting the amount of times which each specific skill occurs in a loop and then displaying in the UI through a variable which I am currently working on. I have also thought of the possibility of creating a Class with all the relevant statistics needed which would be easier to count the statistics to later display, however the class instances would be created dynamically after each occurrence and through some searching, this method seems more difficult. Is it worth continuing with the list + array method or can creating class instances be a more effective way?

    Hope you can help!

    Aden
     
    Last edited: Jan 2, 2017
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Honestly, I would not create a list of arrays. The problem with this design is you will have to keep track of what value is in what slot of your array.

    You'd be better off creating a class with either properties or fields. That way you can access it by name rather then trying to figure out that finishPosition is index whatever in the array.

    Plus, if you do create a class you could also add methods to it that may allow you more functions if needed.

    This will make it much better in the long run. It will also help if you want to perform different queries on this info or possibly save the data out as a json or other format.
     
    Kiwasi likes this.
  3. adentutton

    adentutton

    Joined:
    Mar 24, 2013
    Posts:
    34
    Thanks for the quick reply!

    I thought that creating a custom class looked a lot cleaner and would work better in the long run, especially with searching for the specific information later down the track when a lot of information in stored inside. I have a database asset which I will be using to transfer the information to after the match is played. My only concern is the naming of each new instance of the Statistic class. I have done some reading and see that it is not possible to dynamically name the instances when being created. Do you have any ideas or somewhere you could point me to how I would solve this solution?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Why do you need to create several instances of a class?
    You'll be storing them in a list, so you'll be accessing them by index. And if you need to pull one out, you can just assign it to a variable on it's own, but this depends on your use case.
     
  5. adentutton

    adentutton

    Joined:
    Mar 24, 2013
    Posts:
    34
    Thanks for your help! I am currently learning C# so am using a little guess work for what Im trying to achieve! I totally understand what you've explained now and Im sure down the track it is going to help me!
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Throw a name field into the Statistics class. And if you need to access each instance by name, use a dictionary.
     
    Brathnann likes this.
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Good point. As @BoredMormon pointed out. If you really have a need to get the instances. A unique ID can be added to the class, or you can use a dictionary instead.
     
  8. adentutton

    adentutton

    Joined:
    Mar 24, 2013
    Posts:
    34
    At the moment that is what the statID is, and is increased after adding the information to the list