Updated BaseItem, InventoryComponent.cpp

Fixed an issue where the final item was not dissapearing, added item stacking so when purchasing the same item it will stack instead of being seperate instances.
This commit is contained in:
MH261677 2023-01-12 15:21:32 +00:00
parent 3710f33e74
commit cd745b086b
2 changed files with 27 additions and 9 deletions

View File

@ -39,13 +39,27 @@ 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;
}
@ -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();
}

View File

@ -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")