0% found this document useful (0 votes)
9 views

d

The document defines an inventory system in Unreal Engine using C++. It includes a structure for inventory items and a class that allows adding and removing items from the inventory. The class provides functions to manage the inventory, ensuring items can be updated or removed based on their quantity.

Uploaded by

antocel.iulia75
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

d

The document defines an inventory system in Unreal Engine using C++. It includes a structure for inventory items and a class that allows adding and removing items from the inventory. The class provides functions to manage the inventory, ensuring items can be updated or removed based on their quantity.

Uploaded by

antocel.iulia75
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "InventorySystem.generated.h"

USTRUCT(BlueprintType)
struct FInventoryItem {
GENERATED_BODY()

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FName ItemName;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 Quantity;

FInventoryItem() : ItemName(""), Quantity(1) {}


};

UCLASS()
class MYGAME_API AInventorySystem : public AActor
{
GENERATED_BODY()

public:
AInventorySystem();

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Inventory")


TArray<FInventoryItem> Inventory;

UFUNCTION(BlueprintCallable, Category="Inventory")
void AddItem(FName ItemName, int32 Quantity);

UFUNCTION(BlueprintCallable, Category="Inventory")
bool RemoveItem(FName ItemName, int32 Quantity);
};

// -------------------- InventorySystem.cpp --------------------


#include "InventorySystem.h"

AInventorySystem::AInventorySystem() {}

void AInventorySystem::AddItem(FName ItemName, int32 Quantity)


{
for (FInventoryItem& Item : Inventory)
{
if (Item.ItemName == ItemName)
{
Item.Quantity += Quantity;
return;
}
}
Inventory.Add(FInventoryItem{ItemName, Quantity});
}

bool AInventorySystem::RemoveItem(FName ItemName, int32 Quantity)


{
for (int32 i = 0; i < Inventory.Num(); i++)
{
if (Inventory[i].ItemName == ItemName)
{
if (Inventory[i].Quantity > Quantity)
{
Inventory[i].Quantity -= Quantity;
return true;
}
Inventory.RemoveAt(i);
return true;
}
}
return false;
}

You might also like