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

Need Help with Dictionary

Discussion in 'Scripting' started by pihik, Apr 29, 2022.

  1. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    Hey, I don't know how to solve this problem (first time using dictionary) best way would be using for loop I guess, but idk how it works with dictionaries

    Code (CSharp):
    1.  
    2. public Dictionary<Item, int> itemsInInventory = new Dictionary<Item, int>();
    3.  
    4. public void AddResource(Dictionary<Item, int> itemsToAdd)
    5.     {
    6.         foreach (KeyValuePair<Item, int> itemToAdd in itemsToAdd)
    7.         {
    8.  
    9.             if (itemsInInventory.Count == 0)
    10.             {
    11.                 itemsInInventory.Add(itemToAdd.Key, itemToAdd.Value);
    12.             }
    13.             else
    14.             {
    15.                 if (!itemsInInventory.Keys.Equals(itemToAdd.Key))
    16.                 {
    17.                     itemsInInventory.Add(itemToAdd.Key, itemToAdd.Value);
    18.                 }
    19.                 else
    20.                 {
    21.                     // I NEED TO ADD "itemToAdd.value" TO CERTAIN POSITION IN "itemsInInventory"
    22.                     foreach (Item inventoryItem in itemsInInventory.Keys)
    23.                     {
    24.                         if (inventoryItem.Equals(itemToAdd.Key))
    25.                         {
    26.                             itemsInInventory[inventoryItem] += itemToAdd.Value;
    27.                         }
    28.                     }
    29.                 }
    30.             }
    31.                    
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You are wildly overcomplicating this. Just check if it exists or not and add the existing value if it does.
    Code (CSharp):
    1. public void AddResource(Dictionary<Item, int> itemsToAdd) {
    2.    foreach (KeyValuePair<Item, int> toAdd in itemsToAdd) {
    3.       int existingValue = 0;
    4.       // if it exists, use the existing value. Otherwise leave it at 0
    5.       if (itemsInInventory.ContainsKey(toAdd.Key)) {
    6.          existingValue = itemsInInventory[toAdd.Key];
    7.       }
    8.  
    9.       // add the new value
    10.       existingValue += toAdd.Value;
    11.       // write back to the inventory
    12.       itemsInInventory[toAdd.Key] = existingValue;
    13.    }
    14. }
    And here's a slightly more advanced and concise version using TryGetValue:
    Code (CSharp):
    1. public void AddResource(Dictionary<Item, int> itemsToAdd) {
    2.    foreach (KeyValuePair<Item, int> toAdd in itemsToAdd) {
    3.       // grab the existing value or 0 from the inventory.
    4.       itemsInInventory.TryGetValue(toAdd.key, out int existingValue)
    5.       // add the new value
    6.       existingValue += toAdd.Value;
    7.       // write back to the inventory
    8.       itemsInInventory[toAdd.Key] = existingValue;
    9.    }
    10. }
     
    pihik likes this.
  3. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    Perfect, thank you so much!