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:
parent
3710f33e74
commit
cd745b086b
@ -39,13 +39,27 @@ bool UInventoryComponent::AddItem(class UBaseItem* BaseItem)
|
|||||||
}
|
}
|
||||||
BaseItem->StoredItems = this;
|
BaseItem->StoredItems = this;
|
||||||
BaseItem->World = GetWorld();
|
BaseItem->World = GetWorld();
|
||||||
|
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);
|
Items.Add(BaseItem);
|
||||||
UE_LOG(LogTemp, Display, TEXT("ITEM HAS BEEN ADDED TO INVENTORY"));
|
UE_LOG(LogTemp, Display, TEXT("ITEM HAS BEEN ADDED TO INVENTORY"));
|
||||||
//log the itemid
|
}
|
||||||
|
|
||||||
//Refreshes the inventory
|
//Refreshes the inventory
|
||||||
OnInventoryUpdated.Broadcast();
|
OnInventoryUpdated.Broadcast();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +71,7 @@ bool UInventoryComponent::Remove(class UBaseItem* BaseItem)
|
|||||||
UE_LOG(LogTemp, Display, TEXT("ItEM HAS BEEN REMOVED"));
|
UE_LOG(LogTemp, Display, TEXT("ItEM HAS BEEN REMOVED"));
|
||||||
BaseItem->StoredItems = nullptr;
|
BaseItem->StoredItems = nullptr;
|
||||||
BaseItem->World = nullptr;
|
BaseItem->World = nullptr;
|
||||||
//Items.RemoveSingle(BaseItem);
|
Items.RemoveSingle(BaseItem);
|
||||||
OnInventoryUpdated.Broadcast(); // Updates UI
|
OnInventoryUpdated.Broadcast(); // Updates UI
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -71,7 +85,11 @@ UBaseItem* UInventoryComponent::GetItem(int Index)
|
|||||||
|
|
||||||
void UInventoryComponent::RemoveItem(UEatableItems* Item)
|
void UInventoryComponent::RemoveItem(UEatableItems* Item)
|
||||||
{
|
{
|
||||||
Items.RemoveSingle(Item);
|
Item->StackCount--;
|
||||||
|
if (Item->StackCount <= 0)
|
||||||
|
{
|
||||||
|
Remove(Item); // activates the remove function up above
|
||||||
|
}
|
||||||
OnInventoryUpdated.Broadcast();
|
OnInventoryUpdated.Broadcast();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ public:
|
|||||||
bool isDamageBuffItem;
|
bool isDamageBuffItem;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item")
|
||||||
int32 StackCount;
|
int32 StackCount = 1;
|
||||||
|
|
||||||
//reference to the UInventoryComponent script
|
//reference to the UInventoryComponent script
|
||||||
UPROPERTY(EditAnywhere, Category = "Item")
|
UPROPERTY(EditAnywhere, Category = "Item")
|
||||||
|
Loading…
Reference in New Issue
Block a user