Component.h
private:
UPROPERTY(EditAnywhere, Replicated, Category = “Inventory”)
TArray<ASItemActor*> Inventory;
public:
UFUNCTION(BlueprintCallable, Category = “Inventory”)
TArray<ASItemActor*> GetInventory();
Component.cpp
TArray<ASItemActor*> UInventoryContainerComponent::GetInventory()
{
return Inventory;
}
Pickup.cpp
void ASItemPickup::OnUsed(APawn* InstigatorPawn)
{
ASCharacter* MyPawn = Cast<ASCharacter>(InstigatorPawn);
if (MyPawn)
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
ASItemActor* NewItem = GetWorld()->SpawnActor<ASItemActor>(ItemActorClass, SpawnInfo);
//InventoryContainer is the Component
MyPawn->GetInventoryContainer()->GetInventory().Add(NewItem);
//InventoryTest is the same Inventory only directly on the Pawn not in a Component
MyPawn->InventoryTest.Add(NewItem);
NewItem->SetActorLocation(MyPawn->GetActorLocation());
NewItem->SetActorEnableCollision(false);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Added to Inventory.");
Super::OnUsed(InstigatorPawn);
}
}