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

[Solved] Generic List memory address

Discussion in 'Scripting' started by Shnok, Aug 15, 2015.

  1. Shnok

    Shnok

    Joined:
    Aug 14, 2015
    Posts:
    4
    Hi, my problem is really easy. All my values type Item are linked to the value from my ItemData. Indeed, when i modify an item in my inventory, the item in the database, the item in the monster's droplist etc. In this case, this is the quantit. They have the same memory adress.

    I would make a copy of the item not just make a pointer each time i use it. Indeed to avoid this with float or other, i just need to create a new var. So i dont really get it with the generic lists/dictionaries.

    The inventory script:
    Code (CSharp):
    1. public class PlayerInventory : NetworkBehaviour {
    2.     public List<Item> Inventory = new List<Item>();
    3.  
    4.     ....  
    5.     //nearItems the object list on the ground
    6.     AddItem(nearItems[index].GetComponent<ItemObject>().thisItem);
    7.  
    8.     void AddItem(Item groundItem){
    9.         bool foundItem=false;
    10.         int index=0;
    11.  
    12.         Debug.Log (groundItem.Quantity);
    13.  
    14.         for(int i=0;i<Inventory.Count;i++){
    15.             foundItem=true;
    16.             if(groundItem==Inventory[i]) index=i;
    17.         }
    18.  
    19.         if(foundItem==false){
    20.             Inventory.Add (groundItem);
    21.         }else{
    22.             Inventory[index].Quantity+=groundItem.Quantity;
    23.         }
    24.     }
    The script attached to the item gameobject.
    Code (CSharp):
    1. public class ItemObject : MonoBehaviour {
    2.  
    3.     public Item thisItem;
    4.     private bool soundPlayed=false;
    5.     private AudioSource source;
    6.     private Transform myTransform;
    7.     private Collider[] objects = new Collider[100];
    8.     private int layerMask;
    9.  
    10.     void Start(){
    11.         if(thisItem.Quantity==0) thisItem.Quantity=1;
    12.        ...
    13.     }
    14. ...
    The script from the item dropper:
    Code (CSharp):
    1. public class EntityMainScript : NetworkBehaviour {
    2.  
    3.     public List<Item> DropList = new List<Item>();
    4.        ...
    5.  
    6.     void Start () {
    7.  
    8.         DropList.Add (ItemData.GetItem("Money"));
    9.         DropList.Add (ItemData.GetItem("Money"));
    10.         DropList.Add (ItemData.GetItem("Money"));
    11.         DropList.Add (ItemData.GetItem("Money"));
    12.         DropList.Add (ItemData.GetItem("Money"));
    13.         DropList.Add (ItemData.GetItem("Money"));
    14.         DropList.Add (ItemData.GetItem("Money"));
    15.         DropList.Add (ItemData.GetItem("Money"));
    16.         DropList.Add (ItemData.GetItem("Money"));
    17.         DropList.Add (ItemData.GetItem("Money"));
    18.  
    19.     }
    20.  
    21.     void DropItem(){
    22.         for(int i=0;i<DropList.Count;i++){
    23.             Vector3 randomPos = myTransform.position
    24.                 + new Vector3(Random.Range(-0.75f,0.75f),Random.Range(0.5f,1.2f),Random.Range(-0.75f,0.75f));
    25.             GameObject dropgo = Instantiate(DropList[i].Prefab,randomPos,Quaternion.identity) as GameObject;
    26.             dropgo.transform.name=DropList[i].Name;
    27.             dropgo.GetComponent<ItemObject>().thisItem=DropList[i];
    28.             NetworkServer.Spawn(dropgo);
    29.         }
    30.     }
    31.    
    And then my Itemdata script:
    Code (CSharp):
    1. [System.Serializable]
    2. public class Item
    3. {
    4.     public enum ItemType{Sword,Shield,Consumable,Misc};
    5.     public int ID;
    6.     public string Name;
    7.     public int Quantity;
    8.     public Texture2D Icon;
    9.     public ItemType Type;
    10.     public GameObject Prefab;
    11. }
    12.  
    13. public class ItemData
    14. {
    15.  
    16.     private static Dictionary<string, Item> _table;
    17.  
    18.  
    19.     static ItemData()
    20.     {
    21.  
    22.         _table = new Dictionary<string, Item>();
    23.  
    24.         Item newItem = new Item()
    25.         {
    26.             ID=0,
    27.             Name="Money",
    28.             Icon=null,
    29.             Type=Item.ItemType.Misc,
    30.             Prefab=Resources.Load<GameObject>("Data/Items/Money"),
    31.         };
    32.      
    33.         _table.Add (newItem.Name,newItem);
    34.     }
    35.  
    36.     public static Item GetItem(string name)
    37.     {
    38.         Item temp = new Item();
    39.         if(_table.TryGetValue(name,out temp))
    40.             return temp;
    41.         else
    42.             return null;
    43.     }
    44. }
    If you have any enlightment, thanks ! :)
     
    Last edited: Aug 15, 2015
  2. Shnok

    Shnok

    Joined:
    Aug 14, 2015
    Posts:
    4
    I found a solution with modifying the GetItem method from my ItemData script:

    Code (CSharp):
    1.     public static Item GetItem(string name)
    2.     {
    3.         Item temp;
    4.         if(_table.TryGetValue(name,out temp)){
    5.             return Copy (temp);
    6.         }else{
    7.             return null;
    8.         }
    9.     }
    10.  
    11.     public static Item Copy(Item source){
    12.         Item copy = new Item();
    13.         copy.ID=source.ID;
    14.         copy.Name=source.Name;
    15.         copy.Quantity=source.Quantity;
    16.         copy.Type=source.Type;
    17.         copy.Icon=source.Icon;
    18.         copy.Prefab=source.Prefab;
    19.  
    20.         return copy;
    21.  
    22.     }
    There it is :)