Drag and Drop Shield Issue

I know this topic should’ve been under the 2h, shield visibility and dual-handed weapons, but this topic got closed, so I’m forced to open a new question

I gave a friend of mine a rough demo of my game for him to test out, and the first thing he does, is (miraculously) catch a glitch I never would’ve figured out on my own, turning my face into this:

download

:stuck_out_tongue_winking_eye:

Anyway, the guy tried dragging and dropping my shield into the shield slot whilst he was holding a 2h weapon, and the shield did NOT disarm his 2h weapon, causing the whole shield visibility we had when clicking to equip to re-appear, and I couldn’t figure out why (but I’m tuning the ‘EquipmentSlotUI.AddItems()’ functions for that matter, my guess is that this is where the error is coming from).

I tried fixing it on my own by attempting a bit of coding, but I couldn’t exactly figure out what went wrong (mainly because I’m very rusty on how Drag and Drop works :sweat_smile:). If it helps, here is the NRE:

NullReferenceException: Object reference not set to an instance of an object
GameDevTV.Inventories.Equipment.AddItem (GameDevTV.Inventories.EquipLocation slot, GameDevTV.Inventories.EquipableItem item) (at Assets/GameDev.tv Assets/Scripts/Inventories/Equipment.cs:92)
GameDevTV.UI.Inventories.EquipmentSlotUI.CheckLeftHand () (at Assets/GameDev.tv Assets/Scripts/UI/Inventories/EquipmentSlotUI.cs:129)
GameDevTV.UI.Inventories.EquipmentSlotUI.AddItems (GameDevTV.Inventories.InventoryItem item, System.Int32 number) (at Assets/GameDev.tv Assets/Scripts/UI/Inventories/EquipmentSlotUI.cs:110)
GameDevTV.Core.UI.Dragging.DragItem`1[T].AttemptSimpleTransfer (GameDevTV.Core.UI.Dragging.IDragDestination`1[T] destination) (at Assets/GameDev.tv Assets/Scripts/Utils/UI/Dragging/DragItem.cs:173)
GameDevTV.Core.UI.Dragging.DragItem`1[T].DropItemIntoContainer (GameDevTV.Core.UI.Dragging.IDragDestination`1[T] destination) (at Assets/GameDev.tv Assets/Scripts/Utils/UI/Dragging/DragItem.cs:102)
GameDevTV.Core.UI.Dragging.DragItem`1[T].UnityEngine.EventSystems.IEndDragHandler.OnEndDrag (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/GameDev.tv Assets/Scripts/Utils/UI/Dragging/DragItem.cs:73)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IEndDragHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:85)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:514)

And here is my current attempt at coding a solution for it (I labelled line 129 in ‘EquipmentSlotUI.cs’ through comments, where the NRE is coming from):

// Corner case: Add a condition in here that checks if the player is holding a 2h sword,
        // and he's trying to equip (through drag and drop) a shield. If he is, disarm his 2h sword first,
        // and then place the shield into the appropriate slot:

        public void AddItems(InventoryItem item, int number)
        {
            CheckLeftHand();
            playerEquipment.AddItem(equipLocation, (EquipableItem) item);
        }

        private void CheckLeftHand() 
        {
            Equipment equipment = GameObject.FindGameObjectWithTag("Player").GetComponent<Equipment>();
            WeaponConfig otherWeapon = (WeaponConfig)equipment.GetItemInSlot(EquipLocation.Weapon);
            ShieldConfig otherShield = (ShieldConfig)equipment.GetItemInSlot(EquipLocation.Shield);

            if (otherWeapon.IsTwoHanded || otherWeapon.IsLeftHanded)
            {
                if (!inventory.HasSpaceFor(equipment.GetWeaponAndShieldAsIEnumerable()))
                {
                    // Add the weapon back into the inventory (still thinking this one through)
                    return;
                }

                equipment.RemoveItem(EquipLocation.Weapon);
                equipment.AddItem(equipLocation, otherShield);  // line 129, for the NRE
                return;

            }
        }

(This was worth 2 hours of trying out, before my brain fried out). Unfortunately this solution caused not only my shield to permanently disappear, but also my weapon to vanish, like poof.

I want to try and solve this, but for drag and drop, is ‘AddItems()’ really where I should even begin trying to solve this issue from, or should I go to ‘Fighter.UpdateWeapon()’ and tune that function instead?

I’m pretty sure the Drag and Drop part was not complete, largely because I haven’t figured it out quite yet. While it’s easy to PREVENT equipping the shield with a 2H weapon equipped, there are other considerations when doing a force unequip… For the time being, I do not have a solution for this (part of the reason why I didn’t want to go down that path in the first place).

No worries Brian. I’ll keep trying as I (for now) got nothing better to do. If this persists as an issue though, then I’ll make my game UI as toggling layers to make this scenario impossible to meet with in the first place :slight_smile:

eventually I’ll just eliminate the drag and drop system in favor of the click one, mainly because of the bugs…

Privacy & Terms