Update component TArray after editor changes

Hey guys!
I’d like to update a TArray with the right number of UInstancedStaticMeshComponent when I change a value in the details panel.

For now, I can get the TArray to be initialized with the expected number of components when we look at the default values assigned to variables in the header. But then, when I change the values of those same variables in the editor, the TArray of components is not updated accordingly. Here is some code:

in the header:

private:
UPROPERTY(VisibleAnywhere, Category = "Instanced Mesh")
		TArray<UInstancedStaticMeshComponent*> GridMeshChunks;

public: UPROPERTY(EditAnywhere, Category = "Grid Properties", meta = (ClampMin = "1", ClampMax = "10", UIMin = "1", UIMax = "10"))
		int gridLength = 2;
	UPROPERTY(EditAnywhere, Category = "Grid Properties", meta = (ClampMin = "1", ClampMax = "10", UIMin = "1", UIMax = "10"))
		int gridWidth = 2;
	UPROPERTY(EditAnywhere, Category = "Grid Properties", meta = (ClampMin = "1", ClampMax = "10", UIMin = "1", UIMax = "10"))
		int gridHeight = 2;

in the .cpp

AGridManager::AGridManager()
{
	PrimaryActorTick.bCanEverTick = true;

	bReplicates = true;
	bAlwaysRelevant = true;

	int chunkNum = gridLength*gridWidth*gridHeight;

	GridMeshChunks.Empty();

	for (int i = 0; i < chunkNum; i++)
	{
		FString name = "GridMesh" + FString::FromInt(i);
		GridMeshChunks.Add(CreateDefaultSubobject<UInstancedStaticMeshComponent>(*FString(name)));
	}

	RootComponent = GridMeshChunks[0];
	for (int i = 1; i < chunkNum; i++)
	{
	GridMeshChunks[i]->SetupAttachment(RootComponent);
	}
}

So, in this case, I obtain an array with 8 UInstancedStaticMeshComponents. Nothing happens when I change the value in-editor. I believe for something to happen I’d need to add something to the OnConstruction script. I tried to create the components similarly in that function but I couldn’t find a way to make it work without crashes. Any pointers or tips?

I had a similar problem recently. You need to implement the PostEditChangeProperty event. Then you should be able to handle variable changes in the editor.

Thanks for the answer! I got the rest of the way from there.

void AGridManager::PostEditChangeProperty(struct FPropertyChangedEvent& e)
{
	Super::PostEditChangeProperty(e);

	FName PropertyName = (e.Property != NULL) ? e.Property->GetFName() : NAME_None;

	if (PropertyName == GET_MEMBER_NAME_CHECKED(AGridManager, gridLength))
	{
		int chunkNum = gridLength*gridWidth*gridHeight;
		nodeNumber = chunkNum*FMath::Pow(chunkSize, 3);

		for (int i = 0; i < GridMeshChunks.Num(); i++)
		{
			GridMeshChunks[i]->DestroyComponent();
		}

		GridMeshChunks.Empty();

		for (int i = 0; i < chunkNum; i++)
		{
			FString name = "GridMesh" + FString::FromInt(i);
			GridMeshChunks.Add(NewObject<UInstancedStaticMeshComponent>(this, *FString(name)));
			//GridMeshChunks.Add(CreateDefaultSubobject<UInstancedStaticMeshComponent>(*FString(name)));
		}

		RootComponent = GridMeshChunks[0];
		for (int i = 1; i < chunkNum; i++)
		{
			GridMeshChunks[i]->SetupAttachment(RootComponent);
		}
	}
}

This function allows me to change the size and contents of my array on the go. However, the actual components under the hierarchy tree of the actor don’t match up with the array. It looks like I’d need to run another function so the SetupAttachment functions take effect. Any ideas?