Search Unity

Identical dictionaries copying eachother

Discussion in 'Scripting' started by Thegusse, May 3, 2014.

  1. Thegusse

    Thegusse

    Joined:
    Nov 29, 2012
    Posts:
    49
    I've tried to solve this seemingly simple problem for hours now.

    I have a dictionary called curInventory which I want to create a temporary copy of. But for some weird reason, everytime I change the temporary copy, the changes automatically gets applied to the original one as well. Here's the code:

    Code (csharp):
    1.  
    2. // check that all neccessary items exist in inventory
    3. function CheckItems () : boolean {
    4.  
    5.     // create a temporary inventory to check items
    6.     tempInventory = new Dictionary.<GameObject,int>();
    7.    
    8.     tempInventory = inventory.curInventory;
    9.    
    10.     // iterate through all needed recipes
    11.     for (var recipe : GameObject in recipeList){
    12.    
    13.         if (tempInventory.ContainsKey(recipe)  tempInventory[recipe] > 0){
    14.            
    15.             // remove item from list
    16.             tempInventory[recipe] -= 1; // when I change tempInventory here, it also changes the original dictionary
    17.            
    18.         }
    19.        
    20.         else {
    21.            
    22.             // not all items are existing, return false
    23.             return false;
    24.         }
    25.     }
    26.    
    27.     // everything exists in inventory, return true
    28.     return true;   
    29.    
    30. }
    31.  
    Is there some kind of magical rule with dictionaries that I'm missing, or have I just made a stupid error somewhere else in the code?

    Thanks.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Probably the same issues that i've already tried to explain here.

    You just set your variable tempInventory to reference a new object, this is almost correct. But one line later, you tell tempInventory to reference your curInventory. This does only work with data types such as int, float etc but also structs.
    However, classes are reference types, so these both now reference the exact same object and both can be used in order to alter it, which you do not want in this case.
     
  3. Thegusse

    Thegusse

    Joined:
    Nov 29, 2012
    Posts:
    49
    I see.

    But since I can't write:
    Code (csharp):
    1.  
    2. tempInventory = new inventory.curInventory
    3.  
    How should I make a new object that's still identical? I guess I could set each value through a for-loop, but I figure there's an easier way?
     
  4. Deleted User

    Deleted User

    Guest

    Code (csharp):
    1.  
    2.  
    3. tempInventory = new Dictionary.<GameObject,int>( inventory.curInventory );
    4.  
    5.  
    should work
     
  5. Thegusse

    Thegusse

    Joined:
    Nov 29, 2012
    Posts:
    49
    Works like a charm.

    Thanks for the help guys. Would never have found out otherwise :)