아이템을 인벤토리에서 밖으로 꺼내는 과정을 구현해볼 것이다.
// Inv_InventoryComponent.h
UPROPERTY(EditAnywhere, Category = "Inventory")
float DropSpawnAngleMin = -85.f;
UPROPERTY(EditAnywhere, Category = "Inventory")
float DropSpawnAngleMax = 85.f;
UPROPERTY(EditAnywhere, Category = "Inventory")
float DropSpawnDistanceMin = 50.f;
UPROPERTY(EditAnywhere, Category = "Inventory")
float DropSpawnDistanceMax = 125.f;
UPROPERTY(EditAnywhere, Category = "Inventory")
float RelativeSpawnElevation = 70.f;
// Inv_InventoryComponent.cpp
void UInv_InventoryComponent::Server_DropItem_Implementation(UInv_InventoryItem* Item, int32 StackCount)
{
const int32 NewStackCount = Item->GetTotalStackCount() - StackCount;
if (NewStackCount <= 0)
{
InventoryList.RemoveEntry(Item);
}
else
{
Item->SetTotalStackCount(NewStackCount);
}
SpawnDroppedItem(Item, StackCount);
}
void UInv_InventoryComponent::SpawnDroppedItem(UInv_InventoryItem* Item, int32 StackCount)
{
const APawn* OwningPawn = OwningController->GetPawn();
FVector RotatedForward = OwningPawn->GetActorForwardVector();
RotatedForward = RotatedForward.RotateAngleAxis(FMath::FRandRange(DropSpawnAngleMin, DropSpawnAngleMax), FVector::UpVector);
FVector SpawnLocation = OwningPawn->GetActorLocation() + RotatedForward * FMath::FRandRange(DropSpawnDistanceMin, DropSpawnDistanceMax);
SpawnLocation.Z -= RelativeSpawnElevation;
const FRotator SpawnRotation = FRotator::ZeroRotator;
FInv_ItemManifest& ItemManifest = Item->GetItemManifestMutable();
if (FInv_StackableFragment* StackableFragment = ItemManifest.GetFragmentOfTypeMutable<FInv_StackableFragment>())
{
StackableFragment->SetStackCount(StackCount);
}
ItemManifest.SpawnPickupActor(this, SpawnLocation, SpawnRotation);
}
InventoryComponent에 위와 같이 함수와 변수들을 추가하였다.
인벤토리에서 아이템을 드랍하면 아이템을 일정범위내에서 랜덤범위에 생성하는 함수이다.
// Inv_SpatialInventory.cpp
FReply UInv_SpatialInventory::NativeOnMouseButtonDown(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
{
ActiveGrid->DropItem();
return FReply::Handled();
}
또한 아이템을 클리 후 인벤토리 밖으로 클릭해서 간단히 드랍할 수 있도록 위와같은 함수도 추가로 넣었다.
Drop 버튼으로 드랍이 잘 되며 인벤토리 밖을 클릭해서도 드랍이 잘 되는 것을 볼 수 있다.
'언리얼 - C++ 프로젝트' 카테고리의 다른 글
| GAS_System (1) Rider 및 캐릭터 생성 (0) | 2025.10.19 |
|---|---|
| Inventory System (9) 아이템 설명창 (1) | 2025.08.07 |
| Inventory System (7) 아이템 나누기 (0) | 2025.08.05 |
| Inventory System (6) 아이템 위치 바꾸기 및 합치기 (1) | 2025.08.05 |
| Inventory System (5) 아이템 스택 (3) | 2025.08.03 |