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

@ -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<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,22 +25,29 @@ protected:
virtual void BeginPlay() override;
public:
bool AddItem(class UBaseItem* BaseItem); //adds the item to the player
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
UPROPERTY(EditDefaultsOnly, Instanced)
TArray<class UBaseItem*> 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<class UBaseItem*> 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);
};