Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Calculating Percentage complete from List

Discussion in 'Scripting' started by Simpso, Mar 13, 2019.

  1. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    Hi guys, hoping someone can help me with my logic on a piece of script im working on.
    Im currently setting up a small app for myself to track what pokemon ive caught in Pokemon GO.
    I have all of the potential pokemon you can catch in a list.
    Which has the pokemon name, Unique id, group Id and a few other flags.

    My idea is that when i click on a pokemon it will grab the group id for that pokemon and then return all pokemon from that group in a list. This way i can then tick them off easily when ive found them.
    This all works fine.
    Now im adding a metric in to show percentage complete for each group.
    SO if a group has 10 pokemon in it and i tick one off i have completed 10% of that group.

    My approach to doing this was to have a list with each unique id and if that pokemon has been found or not.
    I would then loop through and count each pokemon which has that group id and then count how many say true in the caught bool part of the list.
    Allowing me to work out percentage completion.

    I just wanted to check if looping through the list with a for loop is the best way of doing this or if im doing it the long way around?
    I have pasted a copy of my code on what i was thinking of doing below.
    Any insite or suggestions would be great.

    Thanks
    CODE
    Code (csharp):
    1.  
    2.     void examples(int groupID)
    3.     {
    4.         int totalPokemon = 0;
    5.         int foundCount = 0;
    6.  
    7.         for (int i = 0; i <= DM.catchList.count; i++)
    8.         {
    9.             if (DataManager.catchList[i].groupID == groupID)
    10.             {
    11.                 totalPokemon += totalPokemon;
    12.             }
    13.             if (DataManager.catchList[i].groupID == groupID & DataManager.catchList[i].Catch==true)
    14.             {
    15.                 foundCount  += foundCount;
    16.             }
    17.  
    18.         }
    19.  
    20.         //Calculate found percentage
    21.         foundCount / totalPokemon;
    22.  
    23.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    There are ways using linq to get counts for certain values, but honestly, I don't see anything wrong with just looping through the list. It's not a large list anyways.
     
    Simpso likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    It is certainly the way that makes the most sense.
     
    Simpso likes this.
  4. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    Cool . Just wanted to check.
    I'm newish to c#. In my day job I use sql a lot so im used to directly querying things.

    Wanted to make sure I wasn't missing a trick somewhere and if looping through was kind of a norm with c#/unity.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Well, the right tool for every situation. In this case, a loop works perfectly fine.

    One quick comment on your code, your values will never increment since you're using += to add the value to itself, which starts at 0.

    Use ++ instead to increment by 1.

    totalPokemon++; for example.
     
  6. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    Oh yeah good shout.
    I bashed it together to show an example and messed that bit up. Thanks for the heads up though :)
     
  7. vladfilipenko

    vladfilipenko

    Joined:
    Jan 28, 2020
    Posts:
    2
    thanks for the info :) I really like when computer calculates the percentage with help of command line that can improve my skillz in IT :) . However, I think that usually using command line is taking too much time, so that's why, I am trying to find a different calculators with help of different forums. By the way since I tried to find it, I found an article about "online percentage calculator" that helped me out to find a simple way to calculate salary for my family :)
     
    Last edited: Feb 3, 2020