[Solved] Generic List memory address

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:

public class PlayerInventory : NetworkBehaviour {
    public List<Item> Inventory = new List<Item>();

    ....  
    //nearItems the object list on the ground
    AddItem(nearItems[index].GetComponent<ItemObject>().thisItem);

    void AddItem(Item groundItem){
        bool foundItem=false;
        int index=0;

        Debug.Log (groundItem.Quantity);

        for(int i=0;i<Inventory.Count;i++){
            foundItem=true;
            if(groundItem==Inventory[i]) index=i;
        }

        if(foundItem==false){
            Inventory.Add (groundItem);
        }else{
            Inventory[index].Quantity+=groundItem.Quantity;
        }
    }

The script attached to the item gameobject.

public class ItemObject : MonoBehaviour {

    public Item thisItem;
    private bool soundPlayed=false;
    private AudioSource source;
    private Transform myTransform;
    private Collider[] objects = new Collider[100];
    private int layerMask;

    void Start(){
        if(thisItem.Quantity==0) thisItem.Quantity=1;
       ...
    }
...

The script from the item dropper:

public class EntityMainScript : NetworkBehaviour {

    public List<Item> DropList = new List<Item>();
       ...

    void Start () {

        DropList.Add (ItemData.GetItem("Money"));
        DropList.Add (ItemData.GetItem("Money"));
        DropList.Add (ItemData.GetItem("Money"));
        DropList.Add (ItemData.GetItem("Money"));
        DropList.Add (ItemData.GetItem("Money"));
        DropList.Add (ItemData.GetItem("Money"));
        DropList.Add (ItemData.GetItem("Money"));
        DropList.Add (ItemData.GetItem("Money"));
        DropList.Add (ItemData.GetItem("Money"));
        DropList.Add (ItemData.GetItem("Money"));

    }

    void DropItem(){
        for(int i=0;i<DropList.Count;i++){
            Vector3 randomPos = myTransform.position
                + new Vector3(Random.Range(-0.75f,0.75f),Random.Range(0.5f,1.2f),Random.Range(-0.75f,0.75f));
            GameObject dropgo = Instantiate(DropList[i].Prefab,randomPos,Quaternion.identity) as GameObject;
            dropgo.transform.name=DropList[i].Name;
            dropgo.GetComponent<ItemObject>().thisItem=DropList[i];
            NetworkServer.Spawn(dropgo);
        }
    }

And then my Itemdata script:

[System.Serializable]
public class Item
{
    public enum ItemType{Sword,Shield,Consumable,Misc};
    public int ID;
    public string Name;
    public int Quantity;
    public Texture2D Icon;
    public ItemType Type;
    public GameObject Prefab;
}

public class ItemData
{
  
    private static Dictionary<string, Item> _table;
  

    static ItemData()
    {

        _table = new Dictionary<string, Item>();

        Item newItem = new Item()
        {
            ID=0,
            Name="Money",
            Icon=null,
            Type=Item.ItemType.Misc,
            Prefab=Resources.Load<GameObject>("Data/Items/Money"),
        };
      
        _table.Add (newItem.Name,newItem);
    }
  
    public static Item GetItem(string name)
    {
        Item temp = new Item();
        if(_table.TryGetValue(name,out temp))
            return temp;
        else
            return null;
    }
}

If you have any enlightment, thanks ! :slight_smile:

I found a solution with modifying the GetItem method from my ItemData script:

    public static Item GetItem(string name)
    {
        Item temp;
        if(_table.TryGetValue(name,out temp)){
            return Copy (temp);
        }else{
            return null;
        }
    }

    public static Item Copy(Item source){
        Item copy = new Item();
        copy.ID=source.ID;
        copy.Name=source.Name;
        copy.Quantity=source.Quantity;
        copy.Type=source.Type;
        copy.Icon=source.Icon;
        copy.Prefab=source.Prefab;

        return copy;

    }

There it is :slight_smile: