Array in Component

Hello Guys,

I have a PlayerPawn, and at the Pawn i added a Component where i have a Array.
Now i want to add Actors to this Array (Pick up Items), but this dont work.
The Actor spawns but in the Array nothing appears.

Now i added the same Array direktly to the Character (Pawn) and a second line to the Pickup
to add the Actor to the Character Array, too. The Actor is now listet on the Character Array but not in the Component Array.

Do someone can tell me, why that dont work?
Or someone have a idea?

Do i need to set some special thins on the Component ?

Thanks so far. DarkSoe

Code samples would be nice in order to help you out.

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-&gt;GetInventoryContainer()-&gt;GetInventory().Add(NewItem);

            //InventoryTest is the same Inventory only directly on the Pawn not in a Component
	MyPawn-&gt;InventoryTest.Add(NewItem);

	NewItem-&gt;SetActorLocation(MyPawn-&gt;GetActorLocation());
	NewItem-&gt;SetActorEnableCollision(false);

	GEngine-&gt;AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Added to Inventory.");

	Super::OnUsed(InstigatorPawn);
 }

}

If still needed, the problem is the GetInventory() function on the component is returning a copy of the array, not a reference. So you are adding the item to the copy, not the array in the component. In the actor you are accessing the array variable directly which is why that works.

So change GetInventory() return type to TArray<ASItemActor*>& or alternatively make a AddItemToInventory(ASItemActor*) function which takes the item and adds it to the array internally.

Oh ****, sure, i missed the & -.-
tanks so much. Some times there are to much chars on the screen if u searching for the bug :smiley:

greez DarkSoe

I definitely know the feeling :smiley: