Added Inventory Notification Functionality

This commit is contained in:
Philip W 2023-05-12 23:50:30 +01:00
parent 7a04920197
commit 1254abfd0e
4 changed files with 26 additions and 10 deletions

Binary file not shown.

Binary file not shown.

View File

@ -12,6 +12,9 @@ UInventoryComponent::UInventoryComponent()
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
static ConstructorHelpers::FClassFinder<UUserWidget> InventoryNotificationWidgetClassFinder(TEXT("/Game/Blueprints/Status_UI/InventoryNotification"));
InventoryNotificationWidgetClass = InventoryNotificationWidgetClassFinder.Class;
MaxItemSlots = 10;
}
@ -21,6 +24,9 @@ void UInventoryComponent::BeginPlay()
{
Super::BeginPlay();
InventoryNotificationWidgetInstance = CreateWidget<UUserWidget>(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

View File

@ -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,6 +25,11 @@ protected:
virtual void BeginPlay() override;
public:
UPROPERTY()
TSubclassOf<UUserWidget> 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
@ -36,6 +42,8 @@ public:
UPROPERTY(BlueprintAssignable, Category= "Inventory")
FOnInventoryUpdated OnInventoryUpdated; //This is our delegate
UPROPERTY(BlueprintAssignable, Category= "Inventory")
FOnItemAdd OnItemAdd;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category= "Items")
TArray<class UBaseItem*> Items; // The items currently in the inventory