From 1254abfd0e1d9b8c96972508c627496d63ad5d15 Mon Sep 17 00:00:00 2001 From: PHILIP White Date: Fri, 12 May 2023 23:50:30 +0100 Subject: [PATCH] Added Inventory Notification Functionality --- .../Status_UI/InventoryNotification.uasset | 4 ++-- .../Status_UI/ItemTextNotification.uasset | 4 ++-- .../BaseItems/InventoryComponent.cpp | 10 +++++++++- .../BaseItems/InventoryComponent.h | 18 +++++++++++++----- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Content/Blueprints/Status_UI/InventoryNotification.uasset b/Content/Blueprints/Status_UI/InventoryNotification.uasset index 719219f..4bf9e6c 100644 --- a/Content/Blueprints/Status_UI/InventoryNotification.uasset +++ b/Content/Blueprints/Status_UI/InventoryNotification.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6b90cacc8c02c82507c7fd2fdd42e901575cd9ef94347a25d1aedd17d42ca516 -size 47198 +oid sha256:ec3e07361b0c35f3eab1f12b5961790f03876551c3a63d5b349dfea2722b1db6 +size 125863 diff --git a/Content/Blueprints/Status_UI/ItemTextNotification.uasset b/Content/Blueprints/Status_UI/ItemTextNotification.uasset index 69bbd63..c0873cc 100644 --- a/Content/Blueprints/Status_UI/ItemTextNotification.uasset +++ b/Content/Blueprints/Status_UI/ItemTextNotification.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0b8bcb607d152eaf0449499b9c5f74a6cea2f8f9cdff6d7682544f34bdfe277b -size 61929 +oid sha256:3d0c23c0e73952f572808a8a75f6e9b42ea9c4539d989b3a86d91f26e305aaff +size 68872 diff --git a/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp b/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp index 28670ec..a13909e 100644 --- a/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp +++ b/Source/the_twilight_abyss/BaseItems/InventoryComponent.cpp @@ -11,6 +11,9 @@ UInventoryComponent::UInventoryComponent() // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features // off to improve performance if you don't need them. PrimaryComponentTick.bCanEverTick = true; + + static ConstructorHelpers::FClassFinder InventoryNotificationWidgetClassFinder(TEXT("/Game/Blueprints/Status_UI/InventoryNotification")); + InventoryNotificationWidgetClass = InventoryNotificationWidgetClassFinder.Class; MaxItemSlots = 10; } @@ -21,6 +24,9 @@ void UInventoryComponent::BeginPlay() { Super::BeginPlay(); + InventoryNotificationWidgetInstance = CreateWidget(GetWorld(), InventoryNotificationWidgetClass); + InventoryNotificationWidgetInstance->AddToViewport(); + //activates the AddItem function for every DefaultItem that inherits BaseItem for (auto& BaseItem : DefaultItems) { @@ -44,7 +50,8 @@ bool UInventoryComponent::AddItem(class UBaseItem* BaseItem) //if the item is the same as the item that is being added if (Item->ItemDisplayName.ToString() == BaseItem->ItemDisplayName.ToString()) { - Item->StackCount++; + Item->StackCount += BaseItem->StackCount; + OnItemAdd.Broadcast(BaseItem->ItemDisplayName.ToString(), BaseItem->StackCount); UE_LOG(LogTemp, Display, TEXT("ITEM STACKCOUNT: %d"), Item->StackCount); isNewItem = false; break; @@ -53,6 +60,7 @@ bool UInventoryComponent::AddItem(class UBaseItem* BaseItem) if (isNewItem == true) { Items.Add(BaseItem); + OnItemAdd.Broadcast(BaseItem->ItemDisplayName.ToString(), BaseItem->StackCount); UE_LOG(LogTemp, Display, TEXT("ITEM HAS BEEN ADDED TO INVENTORY")); } //Refreshes the inventory diff --git a/Source/the_twilight_abyss/BaseItems/InventoryComponent.h b/Source/the_twilight_abyss/BaseItems/InventoryComponent.h index 95513ca..9c931e5 100644 --- a/Source/the_twilight_abyss/BaseItems/InventoryComponent.h +++ b/Source/the_twilight_abyss/BaseItems/InventoryComponent.h @@ -9,6 +9,7 @@ //OUR DELEGATE IS CALLED FONINVENTORYUPDATED DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnInventoryUpdated); +DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnItemAdd, const FString&, ItemDisplayName, const int&, StackAmount); UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) class THE_TWILIGHT_ABYSS_API UInventoryComponent : public UActorComponent @@ -24,22 +25,29 @@ protected: virtual void BeginPlay() override; public: - bool AddItem(class UBaseItem* BaseItem); //adds the item to the player + UPROPERTY() + TSubclassOf InventoryNotificationWidgetClass; + UPROPERTY() + UUserWidget* InventoryNotificationWidgetInstance; + bool AddItem(class UBaseItem* BaseItem); //adds the item to the player + bool Remove(class UBaseItem* BaseItem); //removes the item from the player UPROPERTY(EditDefaultsOnly, Instanced) TArray DefaultItems; //Items you start the game with IF YOU WANT YOU CAN JUST NOT USE THIS - + UPROPERTY(EditDefaultsOnly, Category= "Inventory") int32 MaxItemSlots; - + UPROPERTY(BlueprintAssignable, Category= "Inventory") FOnInventoryUpdated OnInventoryUpdated; //This is our delegate + UPROPERTY(BlueprintAssignable, Category= "Inventory") + FOnItemAdd OnItemAdd; UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category= "Items") TArray Items; // The items currently in the inventory - + UFUNCTION(BlueprintCallable, Category= "Inventory") class UBaseItem* GetItem(int Index); @@ -51,7 +59,7 @@ public: UFUNCTION() void RemoveItemID(const int& ItemID); - + UFUNCTION() UBaseItem* GetItemByID(const int& ItemID); };