








TPSPlayer.h
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
class UInventoryActorComponent* DefaultInventoryTest;
TPSPlayer.cpp
ACTPSPlayer::ACTPSPlayer()
{
... 생략
DefaultInventoryTest = CreateDefaultSubobject<UInventoryActorComponent>(TEXT("DefaultInventoryComp"));
DefaultInventoryTest->MaxInventorySize = 30;
}

InventoryActorComponent.h
publci:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
TMap<FName, FInventoryItem> Inventory;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
UDataTable* ItemDataTable;
UFUNCTION(BlueprintCallable, Category = "Inventory")
FItemData LoadItemFromDataTable(FName ItemRowName);
UFUNCTION(BlueprintCallable, Category = "Inventory")
void AddItemToInventory(FName ItemRowName, int32 Quantity);
UFUNCTION(BlueprintCallable, Category = "Inventory")
void RemoveItemFromInventory(FName ItemRowName, int32 Quantity);
InventoryActorComponent.cpp
FItemData UInventoryActorComponent::LoadItemFromDataTable(FName ItemRowName)
{
if (!ItemDataTable)
{
UE_LOG(LogTemp, Warning, TEXT("Invalid DataTable"));
// 기본 값 반환
return FItemData();
}
// DataTable에서 RowName에 해당하는 데이터를 찾습니다
FItemData* ItemData = ItemDataTable->FindRow<FItemData>(ItemRowName, TEXT("Item Lookup"));
if (ItemData)
{
// 유효한 데이터 반환
return *ItemData;
}
else
{
// 기본값 반환
return FItemData();
}
}
void UInventoryActorComponent::AddItemToInventory(FName ItemRowName, int32 Quantity)
{
// 인벤토리가 가득 찼는지 확인
if (IsInventoryFull())
{
UE_LOG(LogTemp, Warning, TEXT("Inventory is full"));
return;
}
// 이미 인벤토리에 해당 아이템이 있는지 확인
if (Inventory.Contains(ItemRowName))
{
FInventoryItem& ExitingItem = Inventory[ItemRowName];
// 아이템이 스택 가능할 경우 수량을 추가
if (ExitingItem.ItemData.bIsStackable)
{
ExitingItem.Quantity += Quantity;
UE_LOG(LogTemp, Warning, TEXT("Increased quantity of item with ID %s to %d "), *ItemRowName.ToString(), Quantity);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Item with ID %d is not stackable!"), *ItemRowName.ToString());
}
}
else
{
// DataTable에서 해당 RowName의 아이템을 로드하여 추가
if (ItemDataTable)
{
// DataTable 에서 해당 ID의 아이템을 로드하여 추가
FItemData ItemData = LoadItemFromDataTable(ItemRowName);
FInventoryItem NewItem;
NewItem.ItemData = ItemData; // 기본 데이터 설정
NewItem.Quantity = Quantity;
Inventory.Add(ItemRowName, NewItem);
UE_LOG(LogTemp, Warning, TEXT("Added new item with ID %s, Quantity : %d "), *ItemRowName.ToString(), Quantity);
}
}
}
void UInventoryActorComponent::RemoveItemFromInventory(FName ItemRowName, int32 Quantity)
{
// 인벤토리에서 해당 아이템이 존재하는지 확인
if (Inventory.Contains(ItemRowName))
{
FInventoryItem& ItemData = Inventory[ItemRowName];
// 아이템의 수량을 감소시키고 0 이하일 경우 제거
ItemData.Quantity -= Quantity;
if (ItemData.Quantity <= 0)
{
Inventory.Remove(ItemRowName);
UE_LOG(LogTemp, Warning, TEXT("Removed item with ID %s from inventory."), *ItemRowName.ToString());
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Decrease quantity of item with ID %s to %d "), *ItemRowName.ToString(), Quantity);
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Item with ID %s not found in inventory. "), *ItemRowName.ToString());
}
}









