From 3710f33e74c13eb49d4c8e5e8da7f28b73b9ae16 Mon Sep 17 00:00:00 2001 From: MH261677 Date: Thu, 12 Jan 2023 13:56:30 +0000 Subject: [PATCH] Updated Inventory System Done it so the items from the inventory now get removed when used instead of one item never being able to be removed --- .../BaseItems/InventoryComponent.cpp | 18 +++++++----------- .../BaseItems/InventoryComponent.h | 3 +++ .../BaseItems/Items/BaseItem.h | 9 +++------ .../BaseItems/Items/EatableItems.cpp | 12 ++++++------ .../PlayerTemp/TempCharacter.h | 7 +++---- 5 files changed, 22 insertions(+), 27 deletions(-) diff --git a/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp b/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp index 8a5a53e..03745de 100644 --- a/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp +++ b/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp @@ -40,11 +40,8 @@ bool UInventoryComponent::AddItem(class UBaseItem* BaseItem) BaseItem->StoredItems = this; BaseItem->World = GetWorld(); Items.Add(BaseItem); - BaseItem->SubItemID++; - UE_LOG(LogTemp, Display, TEXT("ITEM HAS BEEN ADDED")); + UE_LOG(LogTemp, Display, TEXT("ITEM HAS BEEN ADDED TO INVENTORY")); //log the itemid - UE_LOG(LogTemp, Display, TEXT("ITEM ID: %d"), BaseItem->ItemID); - UE_LOG(LogTemp, Display, TEXT("SUBITEM ID: %d"), BaseItem->SubItemID); //Refreshes the inventory OnInventoryUpdated.Broadcast(); @@ -60,13 +57,6 @@ bool UInventoryComponent::Remove(class UBaseItem* BaseItem) UE_LOG(LogTemp, Display, TEXT("ItEM HAS BEEN REMOVED")); BaseItem->StoredItems = nullptr; BaseItem->World = nullptr; - if(BaseItem->SubItemID < Items.Num()) - { - Items.RemoveSingle(BaseItem); - BaseItem->SubItemID --; - UE_LOG(LogTemp, Display, TEXT("ItemRemoved")); - UE_LOG(LogTemp, Display, TEXT("SUBITEM ID: %d"), BaseItem->SubItemID); - } //Items.RemoveSingle(BaseItem); OnInventoryUpdated.Broadcast(); // Updates UI return true; @@ -79,3 +69,9 @@ UBaseItem* UInventoryComponent::GetItem(int Index) return Items[Index]; } +void UInventoryComponent::RemoveItem(UEatableItems* Item) +{ + Items.RemoveSingle(Item); + OnInventoryUpdated.Broadcast(); +} + diff --git a/Source/the_twilight_abyss/BaseItems/InventoryComponent.h b/Source/the_twilight_abyss/BaseItems/InventoryComponent.h index 41476b1..7ecd224 100644 --- a/Source/the_twilight_abyss/BaseItems/InventoryComponent.h +++ b/Source/the_twilight_abyss/BaseItems/InventoryComponent.h @@ -45,5 +45,8 @@ public: UFUNCTION(BlueprintCallable, Category= "Inventory") class UBaseItem* GetItem(int Index); + + UFUNCTION() + void RemoveItem(UEatableItems* Item); }; diff --git a/Source/the_twilight_abyss/BaseItems/Items/BaseItem.h b/Source/the_twilight_abyss/BaseItems/Items/BaseItem.h index 524269a..fa9426c 100644 --- a/Source/the_twilight_abyss/BaseItems/Items/BaseItem.h +++ b/Source/the_twilight_abyss/BaseItems/Items/BaseItem.h @@ -53,14 +53,11 @@ public: UPROPERTY(EditAnywhere, Category = "Item") bool isDamageBuffItem; - UPROPERTY(EditAnywhere, Category = "Item") - int ItemID; - - UPROPERTY(EditAnywhere, Category = "Item") - int SubItemID; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item") + int32 StackCount; //reference to the UInventoryComponent script - UPROPERTY() + UPROPERTY(EditAnywhere, Category = "Item") class UInventoryComponent* StoredItems; //The buy class to purchase the item diff --git a/Source/the_twilight_abyss/BaseItems/Items/EatableItems.cpp b/Source/the_twilight_abyss/BaseItems/Items/EatableItems.cpp index 352aee9..65d911d 100644 --- a/Source/the_twilight_abyss/BaseItems/Items/EatableItems.cpp +++ b/Source/the_twilight_abyss/BaseItems/Items/EatableItems.cpp @@ -3,14 +3,14 @@ #include "EatableItems.h" +#include "BehaviorTree/BehaviorTreeTypes.h" #include "the_twilight_abyss/BaseItems/InventoryComponent.h" #include "the_twilight_abyss/PlayerTemp/TempCharacter.h" UEatableItems::UEatableItems() { - ItemID; - SubItemID; + } void UEatableItems::Use(ATempCharacter* Character) @@ -21,15 +21,15 @@ void UEatableItems::Use(ATempCharacter* Character) { Character->Health += 10; UE_LOG(LogTemp, Display, TEXT("Healed")); + //delete itself + Character->Inventory->RemoveItem(this); + } if(isDamageBuffItem == true) { // need to add the damage buff functionality here UE_LOG(LogTemp, Display, TEXT("Damage Buffed")); - } - if(StoredItems) - { - StoredItems->Remove(this); + Character->Inventory->RemoveItem(this); } } diff --git a/Source/the_twilight_abyss/PlayerTemp/TempCharacter.h b/Source/the_twilight_abyss/PlayerTemp/TempCharacter.h index 972140d..56cc8d3 100644 --- a/Source/the_twilight_abyss/PlayerTemp/TempCharacter.h +++ b/Source/the_twilight_abyss/PlayerTemp/TempCharacter.h @@ -12,10 +12,6 @@ class THE_TWILIGHT_ABYSS_API ATempCharacter : public ACharacter { GENERATED_BODY() - //Player inventory - UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Inventory, meta = (AllowPrivateAccess = "true")) - class UInventoryComponent* Inventory; //Using the InventoryComponent class - public: // Sets default values for this character's properties ATempCharacter(); @@ -31,6 +27,9 @@ public: // Called every frame virtual void Tick(float DeltaTime) override; + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Inventory, meta = (AllowPrivateAccess = "true")) + class UInventoryComponent* Inventory; //Using the InventoryComponent class + // Called to bind functionality to input virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;