Looking for a way to drop all items when bag is dropped?

First of all, I’d say that using Array for the bag is not the best solution, because you can move things around in the bag and some slots may be empty while some slots after the empty ones can be filled, and arrays don’t allow that. It would be a good idea to use Map (Dictionary) type of collection instead.

But be careful in this case: if you put one item in Slot 0, and another item in Slot 2, the Dictionary Length will be 3, not 2, even though Slot 1 is empty, so you should account for that whenever you get its Length to see where you want to put your next item.


What I imagine for dropping items: Let’s say you have your main bag that can contain up to 20 items. It’s Dictionary keys will be 0-19.


Case 1: you drop your additional bag.
1) You check how many items (Dictionary Values) it contains (again, simple Length will not always return the correct result since you can move things around in the bag). You may make a temporary array for them.
2) You check if you have empty slots in your main bag.
3) You transfer a number of items from the additional bag to the main one, and the rest you Spawn in front of your character and Clear the temporary array.


Case 2: You pick up a smaller additional bag.

  1. Repeat 1) as per above.
  2. Depending on your preferred logic, you may either leave the starting items in the new bag and transfer the rest to the main bag, or vice versa. I’d say the first one would be more player-friendly.
  3. You transfer your items to the new bag and some to the main bag if it has room available. The rest you drop.



    You can use different Dictionary for different bags, but you can use one for all of them at once. It might be a little tricky if you plan to have more than one additional bag, because if you change Addition Bag 1 for a larger of a smaller one you will have to change the Keys for your subsequent ones, so maybe having a separate Dictionary for each bag would be easier. But again, when you have to look for a quest item, for instance, that you have to remove from the inventory automatically, you will have to look through all the Dictionaries instead of just one. So it depends.