diff --git a/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp b/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp index 03745de..a208bd0 100644 --- a/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp +++ b/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp @@ -39,14 +39,28 @@ bool UInventoryComponent::AddItem(class UBaseItem* BaseItem) } BaseItem->StoredItems = this; BaseItem->World = GetWorld(); - Items.Add(BaseItem); - UE_LOG(LogTemp, Display, TEXT("ITEM HAS BEEN ADDED TO INVENTORY")); - //log the itemid - + bool isNewItem = true; + // for every item in inventory + for (auto & Item : Items) + { + //if the item is the same as the item that is being added + if (Item->ItemDisplayName.ToString() == BaseItem->ItemDisplayName.ToString()) + { + //add the amount of the item that is being added to the item in the inventory + Item->StackCount++; + UE_LOG(LogTemp, Display, TEXT("ITEM STACKCOUNT: %d"), Item->StackCount); + isNewItem = false; + break; + } + } + if (isNewItem == true) + { + Items.Add(BaseItem); + UE_LOG(LogTemp, Display, TEXT("ITEM HAS BEEN ADDED TO INVENTORY")); + } //Refreshes the inventory OnInventoryUpdated.Broadcast(); - - return true; + return true; } // remove only gets called once on the same item @@ -57,7 +71,7 @@ bool UInventoryComponent::Remove(class UBaseItem* BaseItem) UE_LOG(LogTemp, Display, TEXT("ItEM HAS BEEN REMOVED")); BaseItem->StoredItems = nullptr; BaseItem->World = nullptr; - //Items.RemoveSingle(BaseItem); + Items.RemoveSingle(BaseItem); OnInventoryUpdated.Broadcast(); // Updates UI return true; } @@ -71,7 +85,11 @@ UBaseItem* UInventoryComponent::GetItem(int Index) void UInventoryComponent::RemoveItem(UEatableItems* Item) { - Items.RemoveSingle(Item); + Item->StackCount--; + if (Item->StackCount <= 0) + { + Remove(Item); // activates the remove function up above + } OnInventoryUpdated.Broadcast(); } diff --git a/Source/the_twilight_abyss/BaseItems/Items/BaseItem.h b/Source/the_twilight_abyss/BaseItems/Items/BaseItem.h index fa9426c..346966f 100644 --- a/Source/the_twilight_abyss/BaseItems/Items/BaseItem.h +++ b/Source/the_twilight_abyss/BaseItems/Items/BaseItem.h @@ -54,7 +54,7 @@ public: bool isDamageBuffItem; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item") - int32 StackCount; + int32 StackCount = 1; //reference to the UInventoryComponent script UPROPERTY(EditAnywhere, Category = "Item")