Search Unity

Searching list of classes

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

  1. adentutton

    adentutton

    Joined:
    Mar 24, 2013
    Posts:
    34
    Hey guys

    I currently have a sports sim which stores its statistics in classes which contains all the information needed (e.g. where the hit went, result of the hit, who hit it, what stage of the game was the hit performed etc.) Im storing these class instances in a list and want to search these lists as a way to display the stats during the match. I have been playing around with some loops and found some information through searching of using Linq but saw it doesn't have the best performance and Im working on predominantly a mobile game!

    Any help pointing me out in the right direction would be much appreciated!

    Aden
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Don't overthink it: just write the code to iterate the classes and do the sums/tallies you need. I can't imagine you'll run into performance issues until you have hundreds of thousands of items to iterate over. Computers are pretty fast. :)
     
    Ryiah, Kiwasi and lordofduct like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Plus one for trying the nieve approach first. Often that's enough.

    Also figure out how you can cache results. A search can take as long as it needs to if you only ever do it once.
     
    Kurt-Dekker likes this.
  4. adentutton

    adentutton

    Joined:
    Mar 24, 2013
    Posts:
    34
    Thanks guys! At the moment it looks like this

    Code (CSharp):
    1. foreach (var stat in statistics) { // statistics is the list of classes
    2.  
    3.             if (stat.playerID == 6) { //ability to add various or statements (playerId + skillType/SkillResult)?
    4.  
    5.             statisticOutput.Add(stat);
    6.             print(statisticOutput.Count);
    7.  
    8.             }
    9.  
    10.         }
    If I have other conditions which I want to count is it as easy as just adding the various conditions I need to the loop? Also is it easy enough to grab the index of the list position of the class from this equation?