I'm try to do the C++ equivalent of Construct object from class in blueprint but doesn't seem to be working.

As the title says, I’m trying to construct this object arry, but when I go see if anything was added, it’s reads none and says both properties are invalid.

void USpatialInventory::initializeinventory()
{

AExtractionShooterCharacter* PlayerChar = Cast<AExtractionShooterCharacter>(GetOwner());

if (!PlayerChar)
{
	UE_LOG(LogTemp, Warning, TEXT("USpatialInventory owner is not AExtractionShooterCharacter!"));
	return;
}

if (!ContainerItemClass)
{
	UE_LOG(LogTemp, Warning, TEXT("ContainerItemClass is not valid!"));
	return;
}


//Spawn the item using the class
UContainerItem* Item = NewObject<UContainerItem>(this, ContainerItemClass);
Item->SetDimension(FIntPoint(1, 4));
Item->SetName(FName(TEXT("Pockets")));
Item->SetIcon(nullptr);
Item->SetIconRotated(nullptr);

if (!ContainerCellClass)
{
	UE_LOG(LogTemp, Warning, TEXT("ContainerCellClass is not valid!"));
	return;
}



//ccreate the cell
UContainerCell* Cell = NewObject<UContainerCell>(this, ContainerCellClass);
Cell->SetDimension(DefaultContainerDimension);
Cell->SetUsable(true);

//Create a default pocket
TArray<UContainerItem*> DefaultLocalContainerItems;

DefaultLocalContainerItems.SetNum(DefaultContainerDimension.X * DefaultContainerDimension.Y);
Cell->SetItems(DefaultLocalContainerItems);




TArray<UContainerCell*> Cells;
Cells.Add(Cell);


FContainerRowStuct Row;
Row.Cells = Cells;


TArray<FContainerRowStuct> ContainerRows;
ContainerRows.Add(Row);


Item->SetContainerRows(ContainerRows);


Items.Add(Item);

}

public:
// Sets default values for this component’s properties
USpatialInventory();

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Inventory")
TSubclassOf<UContainerItem> ContainerItemClass;

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Inventory")
TSubclassOf<UContainerCell> ContainerCellClass;

a lot of these are just local variables, are you actually setting it on the object?

It’s too bad we don’t get to know what Items is. As this is the property that is at the center of your issue :cry:

But if it were a TArray that is not declared as a UPROPERTY, I bet that its contents would be garbage collected at the next GC cycle and that it would therefore read as invalid, unless you look quickly enough. So this could be your solution: Make sure that this TArray is a UPROPERTY.

For this reason, I highly recommend bumping up the GC frequency very high (= every few seconds) in the editor, so that you can catch such issues without having to do a long PIE testing session.

Something else is also bothering me: You’re saying “both properties are invalid”, but I don’t see what “both” could be referring to, as only “Items” seems to be directly related to the idea of the object array you’re trying to fill.

And then I notice that you included a bit of code from the SpatialInventory.h, which should not be of particular interest to us. And this code that shouldn’t be worth posting, contains specifically two properties, the classes for Container Items and Container Cells.

This is not where you should look to see if your array is filled, but if for some reason you were to look there and see that their values are “invalid”, then obviously you would never be able to instanciate anything using these classes, and therefore the array would never get filled.