From a8e4aca91ebe94112782d24d48abeebcc83dc394 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Sat, 12 Oct 2024 17:01:07 -0700 Subject: [PATCH] Make Inventory remove item from hotbar as well Inventory.Remove(ItemMetadata) removes the item from both the inventory and the hotbar --- Items/Inventory.cs | 15 +++++++++++++++ Items/Item.cs | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/Items/Inventory.cs b/Items/Inventory.cs index 825c5bf..a385fc7 100644 --- a/Items/Inventory.cs +++ b/Items/Inventory.cs @@ -239,6 +239,21 @@ public partial class Inventory : Node2D, IItemCollection public bool Remove(ItemMetadata item) { + int indexInInventory = Items.IndexOf(item); + if (indexInInventory < 0) + { + return false; + } + + // remove instances of item from hotbar + int indexInHotbar = HotbarToItemIndexMap.IndexOf(indexInInventory); + if (indexInHotbar >= 0) + { + HotbarToItemIndexMap[indexInHotbar] = -1; + Hotbar[indexInHotbar].QueueFree(); + } + + Items[indexInInventory] = null; return Items.Remove(item); } diff --git a/Items/Item.cs b/Items/Item.cs index c38fc0b..f6d2af6 100644 --- a/Items/Item.cs +++ b/Items/Item.cs @@ -65,4 +65,15 @@ public abstract partial class Item : Node2D { } + + public virtual void Remove() + { + if (IsUsing) + { + Deuse(); + } + + Unequip(CharacterOwner); + QueueFree(); + } }