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. Dismiss Notice

PokéDex-like Enemy list problem

Discussion in 'Scripting' started by Masashi86, Dec 3, 2015.

  1. Masashi86

    Masashi86

    Joined:
    Jul 9, 2014
    Posts:
    7
    Hey, I'm creating an enemy list thing in my game, something similar to concept of PokéDex in Pokémon, where is listed more info about enemies contacted, I call it Enemy Encyclopedia.

    I have problem with the code at the moment, where it adds a new entry to the encyclopedia's list, but it keeps adding new entries even they are already known.

    Functionality goes pretty much so that, when enemy is destroyed, it calls the function (ContactCheck(GameObject enemy)) to check if this enemy already exists in this list, if not, then add it to the list. (later on, it would output the enemy info in to a game menu)

    Here's the code so far:
    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class EncyclopediaEntry
    4. {
    5.     public string enemyName;
    6.     public GameObject enemyEntry;
    7.     public bool isContacted;
    8. }
    9.  
    10. public class EnemyEncyclopediaManager : MonoBehaviour
    11. {
    12.     List<EncyclopediaEntry> fullEncyclopedia = new List<EncyclopediaEntry>(); // comparing this with the player's run's list if new contacted enemies found
    13.     public List<EncyclopediaEntry> encyclopediaEnemy = new List<EncyclopediaEntry>();
    14.  
    15.     public void ContactCheck(GameObject enemy) // should check if entry exists, and adds it to the list if same entry isn't found in the list
    16.     {
    17.         EncyclopediaEntry entry = new EncyclopediaEntry();
    18.         entry.enemyEntry = enemy;
    19.         entry.enemyName = enemy.GetComponent<EnemyManager>().enemyName;
    20.         entry.isContacted = false;
    21.  
    22.         if(encyclopediaEnemy.Count ==0)
    23.         {
    24.             encyclopediaEnemy.Add(entry);
    25.             entry.isContacted = true;
    26.             Debug.Log (entry.enemyName + " discovered!");
    27.         }else
    28.         {
    29.             for (int i = 0; i < encyclopediaEnemy.Count; i++)
    30.             {
    31.                 if (entry.enemyName == encyclopediaEnemy[i].enemyName)
    32.                 {
    33.                     Debug.Log ("Enemy found already!");
    34.                     return;
    35.                 }else
    36.                 {  
    37.                     if(entry.isContacted ==false)
    38.                     {
    39.                         encyclopediaEnemy.Add(entry);
    40.                         entry.isContacted = true;
    41.                         Debug.Log (entry.enemyName + " discovered!");
    42.                     }else
    43.                     {
    44.                         Debug.Log ("Enemy found already!");
    45.                         return;
    46.                     }
    47.  
    48.                 }
    49.             }              
    50.  
    51.         }
    52.  
    53.         FUllListCheck (entry);
    54.     }
    55.  
    56.     public void FUllListCheck(EncyclopediaEntry entry)
    57.     {
    58.         for (int i = 0; i < fullEncyclopedia.Count; i++)
    59.         {
    60.             if(fullEncyclopedia.Contains (entry) && entry.isContacted == true)
    61.             {
    62.                 if(fullEncyclopedia[i].isContacted != true)
    63.                 {
    64.                     fullEncyclopedia[i].isContacted = true;
    65.                 }else
    66.                 {
    67.                     return;
    68.                 }
    69.  
    70.             }else
    71.             {
    72.                 return;
    73.             }
    74.         }
    75.     }
    76. }
    I cannot seem to find a good solution to overcome this problem. If you have any suggestions how should I approach this problem I'd be grateful :)
     
  2. michaelhill

    michaelhill

    Joined:
    Nov 4, 2015
    Posts:
    10
    I would try using a Dictionary instead of a list. This will allow you to find an enemy by name without needing a for loop. You can set up a dictionary as follows:
    Code (CSharp):
    1. Dictionary <string, EncyclopediaEntry> enemyDictionary = new Dictionary<string, EncyclopediaEntry>();
    To add an entry to your dictionary, you'll need to write a for loop to populate it:
    Code (CSharp):
    1. for(int i = 0; i < fullEncyclopedia.Count, i++){
    2.     enemyDictionary.add(fullEncyclopedia[i].enemyName, fullEncyclopedia[i]);
    3. }
    So, once your list is set up, you can easily check to see if an enemy has been encountered and update it:
    Code (CSharp):
    1. if(enemyDictionary[entry.name].isContacted == false){
    2.     //Enemy has not been found
    3.     enemyDictionary[entry.name].isContacted = true;
    4. }
    5. else{
    6.     //Enemy has been found
    7. }
    To find a list of all enemies you have encountered, you need to include Linq in your project:
    Code (CSharp):
    1. using System.Linq;
    Once done, you should be able to search your dictionary and get a Dictionary of all enemies that have been encountered like this:
    Code (CSharp):
    1. Dictionary<string, EncyclopediaEntry> allContactedEnemies = enemyDictionary.Where(x => x.isContacted == true);