After following the Making Inventory Containers (Banks, NPC loots, Treasure Chests) post, I tried to make it so that I could have certain Inventories that could only accept a certain type of items.
I gave this a try by adding adding simple bool in my Inventory.cs script, like isToolbox. I then went to the AddToFirstEmptySlot and AddItemToSlot methods and I simply copied what was already inside, added an if statement for if (isToolbox) and pasted the logic in there. Also, before that I made sore that i also added a check for if the item is of type Tool. So far so good.
Next I added an else inside, to handle items that are not of that type, and I simply just did a RemoveItem() and AddToFirstEmptySlot() after, the AddToFirstEmptySlot is pointing to my Players inventory, so it would know where exactly to put it.
When I went ahead and tested it, it actually worked. But my issue is that, it kind of seems to have some kind of major impact on the game performance. What I mean is, after using the Profiler, I saw that each time, even while in my own player inventory, when I do a simple item drag and drop, there is a spike in performance, where for a split second, the frames drop by ~20-30, and it’s like a micro stutter that you can’t really see it. you can notice it if for example you have a particle effect playing somewhere on the map, and you can see the animation skipping for that split second. I saw that the spike comes from the face that the game has to Instantiate and do a bunch of other things when you perform that action.
Now my biggest problem comes when I start moving items in that “Toolbox” container. If I move one item in, it’s ok, but if I start putting more items in, it will make the game stutter really noticeably when I perform a drag and drop within that container. If I move them back into my inventory, it seems to not do that anymore, the stutter is minimal. But if I try to put an item that can’t go into that container, it will start to produce this issue, for the items in that container.
I then tried to take a look in the DragItem.cs script, since I remembered that’s where we perform the item dragging and dropping.
I found the methods that took care of that, and basically what it does is the same thing, if you try to move the item into into a slot that either can’t take the item, or is not valid it does the AttemptSimpleTransfer method, where inside it removes and then adds the item. Now I don’t know if those 2 methods are the same as RemoveItem and AddToFirst slot from the inventory script, because those methods come from the IDragDestination and IDragSource scripts.
I thought that maybe I could somehow add a check into the DragItem.cs script, and have the item that is not able to go into that inventory container slot, act exactly like it does when you try to equip an equipment item into the wrong equipment slot.
I took a look inside the scripts but I couldn’t figure out where to do it or how to do it.
My question is, if there is a more simpler way and better way to do this? And a way that does not produce the performance issues that my way did.
Would appreciate if someone could help out.
Thanks