UPROPERTY() TArray of UStaticMeshComponent Invalid Size?

I’m not sure how this is possible, but I must be doing something wrong. I want to create a bunch of StaticMeshComponents - and don’t want to have to declare a specific variable for each one. To make my life easier, I’ve done the following in the Header file:

/* Meshes that make up the visual component of Earth */
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Earth Components")    TArray<UStaticMeshComponent*> EarthSurfaceMeshes;

Then in the CPP File, in my constructor - I have a loop that basically does this:

       EarthSurfaceMeshes[Idx] = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, *UniqueObjectName);
        EarthSurfaceMeshes[Idx]->SetMobility(EComponentMobility::Movable);
        EarthSurfaceMeshes[Idx]->SetAbsolute(false, false, true);
        EarthSurfaceMeshes[Idx]->SetupAttachment(GetEarthMesh());
        EarthSurfaceMeshes[Idx]->SetWorldRotation(SpawnRotation);
        EarthSurfaceMeshes[Idx]->SetCollisionEnabled(ECollisionEnabled::NoCollision);
        EarthSurfaceMeshes[Idx]->SetWorldScale3D(FVector(10.f, 10.f, 10.f));
        EarthSurfaceMeshes[Idx]->SetCastShadow(false);
        EarthSurfaceMeshes[Idx]->SetStaticMesh(ResultMesh);
        EarthSurfaceMeshes[Idx]->ComponentTags.Add(*UniqueObjectName);

The EarthSurfaceMeshes array is initialized to 24 elements before this loop runs, but when I try to access the actual meshes in PostInitializeComponents() or OnConstruction() for example, the Array has 18 element, and they are all NULL?

I don’t understand how that can be possible, since all 24 meshes are there and visible :confused:

You can’t just set object on Idx place (I suppose that Idx goes from zero to N), because this place does not exist yet. EarthSurfaceMeshes is a pointer to so far empty structure TArray. If you want to do anything, you need to Add an object. Your code should actually look like:

 EarthSurfaceMeshes.Add(ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, *UniqueObjectName));
//(here goes the same rest)

If everyting is right, you will create an object on Idx place, and your operations will be made on it. But I need to say, it would be even safer to write your code this way:

UStaticMeshComponent *surfMesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, *UniqueObjectName);
surfMesh->SetMobility(EComponentMobility::Movable);
surfMesh->SetAbsolute(false, false, true);
surfMesh->SetupAttachment(GetEarthMesh());
surfMesh->SetWorldRotation(SpawnRotation);
surfMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
surfMesh->SetWorldScale3D(FVector(10.f, 10.f, 10.f));
surfMesh->SetCastShadow(false);
surfMesh->SetStaticMesh(ResultMesh);
surfMesh->ComponentTags.Add(*UniqueObjectName);
EarthSurfaceMeshes.Add(surfMesh);

The error is because of dumped memory. In normal C++ if you make an array, let’s say int *tab; and initialise elements in it by tab[0]=1; tab[1]=2; etc. you will get Memory Dumped immediately.

I did say in the post that the Array was initialized to a certain number of elements before that loop runs.

I resolved the issue in the end. The problem is that the Blueprint version of the object wasn’t being updated when I changed the constructor / post init components in C++ - it was saving redundant data.

Recreating the Blueprint solved the issue.