There's a bug that is removing the equipped item

I’ll add a gyazo GIF to better illustrate the problem:

I’ve been trying to figure this out the last 24 hours without luck. First I thought it was in EquipmentSlotUI.cs in AddItems so I added an if check there but that didn’t work and the Debug.Log also logged before and after the if check so it’s not in that code. I went further to DragItem.cs and thought it might be the DropItemIntoContainer or AttemptSwap methods, but none of those either. So I think it might be the IEndDragHandler.OnEndDrag that should have a check to fix the problem, but I did not manage to fix it. I loaded up the finished RPG course project from GameDev.tv and the issue was also there so it is not any modification that I’ve made.

I’m going to need some clarification…
Is this happening with ANY drag to the Equipment? In other words, if you equip a weapon, or any slot that isn’t duplicated in the UI, does this behavior persist?

I notice that you have two ring slots. Currently, our Equipment.cs isn’t set up to handle two ring slots, as the underlying Equipment is indexing by EquipLocation (through a Dictionary<EquipLocation, InventoryItem>). While this is a deficiency in the system that I look to correct in the future, it does cause strange behaviour if you have multiple EquipslotUI linking to the same EquipLocation.

Yes any items, I’ll put a gyazo GIF here from the original project without any modification to the code. This one is just cloned from GameDev’s github repo:

Regarding the ring slots, that’s true. I noticed it when I setup up two slots for the rings, but that one I managed to fix. I made the modification using a dictionary in Equiptment.cs:
Dictionary<(EquipLocation, string), EquipableItem> equippedItems = new Dictionary<(EquipLocation, string), EquipableItem>();
And a slotIdentifier which gets a GUID when it is being added to the slot to differentiate which slot it is being put to. The slotIdentifier is past through to the methods in Equipment.cs.
Adding another gyazo GIF of it (ignore the boots sprite as a ring item):

So specifically, the error is when you put the wrong type of equipment in the slot.

Are you working off of the final project/inventory.zip in the integration section or the version of the code presented in this lesson?

Any item, even those not equipable will trigger this behaviour.

I am working off from this repo and making tweaks as I go, including updating the bugs and notes that I remember from all the videos:

I’ve recently tested with the inventory.zip and it’s the same behaviour there so I think it might be the inventory in general. I haven’t had the time to debug further yet, but I’m guessing that the item the is trying to be equipped to the equipped slot will remove the item from the equipped slot temporary and then check if the dragged item is compatible or not. If not then the dragged item gets put back to the inventory, but since the already equipped item is removed, it never gets added back. Or something like that…

Went back to the original repo (I have so many clones with so many variations, it’s insane!).

Seeing the bug, and it’s a fairly simple patch: Go into DragItem.cs, and look for the AttemptSwap() method. I put a comment above the section to add, and where to put it. This will filter out attempts to put an impossible item in a slot and return the items to their previous locations and then walk away slowly…

        private void AttemptSwap(IDragContainer<T> destination, IDragContainer<T> source)
        {
            // Provisionally remove item from both sides. 
            var removedSourceNumber = source.GetNumber();
            var removedSourceItem = source.GetItem();
            var removedDestinationNumber = destination.GetNumber();
            var removedDestinationItem = destination.GetItem();
           
            source.RemoveItems(removedSourceNumber);
            destination.RemoveItems(removedDestinationNumber);
            
            // patch:  Add this section to patch swapping bug
            if (destination.MaxAcceptable(removedSourceItem) == 0)
            {
                source.AddItems(removedSourceItem, removedSourceNumber);
                destination.AddItems(removedDestinationItem, removedDestinationNumber);
                return;
            }
            

Hmm, I understand what you’re doing in the if statement, that was actually pretty simple!
I don’t understand the if statement itself.

if (destination.MaxAcceptable(removedSourceItem) == 0)

Is it correct that we are trying to check how many items of the dragged item can be populated to the destination container and since this is an equipment slot which will always have an MaxAcceptable of 1 and since the dragged item is incompatible for the slot it will always return 0 and therefore the if statement will return true?

Btw, thank you very much for the help!

It looks like you do understand the if statement perfectly. :slight_smile:

The check runs after the items are removed because in EquipSlotUI, the MaxAcceptable is always zero if something is already in the slot.

I see, it was a simple solution indeed, but I did not think of the MaxAcceptable of the equipment slot :sweat_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms