Item Drop System - HelpMeGame/Sniffle6-SOInventory-Changes GitHub Wiki

-- Developed by HelpMeGame --

This system saves the data of an item in the inventory, and spawns a prefab on the ground with that data. The object can then be picked up, adding the item along with it's data to the player's inventory.

First, add the following code the the GroundItem class. This will allow us to keep track of more information about the item we are dropping.

    //This can go under "public ItemObject item;"
    public Item ItemData;
    public int amount;
    public bool looted = false;

We can then create a new GameObject, which will be instantiated from a prefab on the item. Then, we transfer all the data from the dropped item into the GameObject, and effectively delete the reference to the item. However, we will also be saving the data of the item, so that the parts that randomize on pickup are the same as when the item was dropped. This next bit needs to be added into your OnDragEnd() function under the UserInterface class, in the spot where the tutorial would have you delete the item.

if (slotsOnInterface[obj].item.Id >= 0)
            {
                Item _item = slotsOnInterface[obj].item;
                ItemObject _itemObj = slotsOnInterface[obj].ItemObj;
                Vector3 pos = new Vector3(Player.Instance.transform.position.x, Player.Instance.transform.position.y - 1, Player.Instance.transform.position.z);
                GameObject loot = Instantiate(_itemObj.prefab, pos, Quaternion.identity);
                loot.AddComponent<GroundItem>().ItemData = _item;
                loot.GetComponent<GroundItem>().item = _itemObj;
                loot.GetComponent<GroundItem>().amount = slotsOnInterface[obj].amount;
            }

            slotsOnInterface[obj].DeleteItem();

            return;

The above script basically assigns the physical data of the item to the ground item. With some more coding in the pickup function, we can then force that data to be used instead of making a brand new Item with new values. First, lets add a reference at the top of the script for _item, so that we can access it from anywhere in the script, in case we would wish to do so later.

Item _item;

We now need to actually change the code to pickup items. We are going to use the looted variable as a way of keeping track of an item, in case it takes multiple frames to loot. Now, add the following to your pickup script in the Player class. (Change OnColliderEnter, to OnTriggerStay);

if (Input.GetKey(KeyCode.F))
            {
                var _groundObject = other.GetComponent<GroundItem>();
                var item = _groundObject;
                if (!item.looted)
                {
                    if (item && item.amount != 0)
                    {
                        item.looted = true;

                        if (item.ItemData.Id >= 0)
                        {
                            _item = item.ItemData;
                        }
                        else
                            _item = new Item(item.item);

                        if (inventory.AddItem(_item, item.amount))
                        {
                            Destroy(other.gameObject);
                        }
                        else
                        {
                            item.looted = false;
                        }
                    }
}
            }

Note: This script will change the way pickups are handled. The default pickup key will be rebound to F, and it stops items from duplicating when you pick them up.

⚠️ **GitHub.com Fallback** ⚠️