Dynamically Created Objects Are Being Removed From Their Arrays

Unreal Version: 5.3.2

I am attempting to create a dynamic mesh creation tool, and currently any component (Static Mesh Components and Text Render Components are examples I have seen affected thus far) created dynamically by function and stored in an array are eventually cleared. I believe this is due to the garbage collection system, but I don’t know why this would be because these components are being stored in arrays shared throughout the blueprint (not locally).

Images included for context.

Above is the function I call in the editor to spawn a new Static Mesh Plane (custom mesh as you can see in the details panel)

For the sake of this post, I clicked the button to call the function 3 times, so 3 meshes were generated (all have the name NODE_AddStaticMeshComponent with a number at the end).

The image above is after moving the Blueprint actor around, and all the names have been changed to TRASH_StaticMeshComponent with unique numbers at the ends. At this point, these components are also no longer valid. This is what leads me to believe that garbage collection is the culprit despite these components being stored in instance editable arrays.

In the image above, this is after I save the map or undo my last action in the editor. As you can see, the components are no longer being stored and nothing is in each of the indexes, which makes it impossible to have multiple dynamic objects created by the user in the engine.

If anyone has some suggestions or knows of a reason why this would be, any information is greatly appreciated.

hellow, welcome to the forum.

thanks for the screenshots, though the text is very small.

have you noticed you arent adding anything to the array?

i think you should connect the return from addstaticmeshcomponent to that next node.

also nice way to put your nodes below the others, i do the same.

another detail is that when you move an object, the object resets its properties and calls again some of the constructors. it’s basically recreating the object everytime you move it or change its properties. so if you want to be able to add the components during editor time, you need to be very careful with whether the array is transient or not and how you reconstruct the components once an edit occurs.

this is a code i used for an object where i can add meshes dynamically at editor time.



void ABooks::OnConstruction(const FTransform& Transform) {
    Super::OnConstruction(Transform);
    // https://forums.unrealengine.com/t/added-components-not-showing-in-details-panel/465572/3?u=nande
    // this is actually the correct place. it has the side effect that it will recreate when a property is changed.
    ReCreate();
    // https://forums.unrealengine.com/t/components-added-at-runtime-dont-show-up-in-editor-details-panel/155064/5?u=nande
}


void ABooks::ReCreate() {
	DestroyBooks();
	CreateBooks();...
}


void ABooks::DestroyBooks() {
	for (UCQuickMesh* const C: Books) {
		if (UNLIKELY(!IsValid(C))) continue;
		RemoveOwnedComponent(C);
		C->DestroyComponent(false);
	}
	Books.Empty();...
}


void ABooks::CreateBooks() {
	// https://forums.unrealengine.com/t/what-is-the-correct-way-to-create-and-add-components-at-runtime/15605/21?u=nande
	const int32 MatMax = Materials.Num() -1;
	for (int32 i =0; i<BookCount; ++i) {
		const FString SName = TEXT("Book_") + FString::FromInt(i);
		UCQuickMesh* const QM = Cast<UCQuickMesh>(
			AddComponentByClass(UCQuickMesh::StaticClass(), true, FTransform::Identity, false));
...
		
		AddOwnedComponent(QM);
		QM->AttachToComponent(Mesh, FAttachmentTransformRules::SnapToTargetIncludingScale);
		QM->SetStaticMesh(BaseMesh);
		...

		Books.Add(QM);
...
		
		UMaterialInterface* const Mat = ....

		QM->SetMaterial(0, Mat);
	}
}


on the header
	UPROPERTY(BlueprintReadOnly, VisibleDefaultsOnly)
	TArray<TObjectPtr<UCQuickMesh>> Books;

Hello, thanks for the welcome.

I apologize, I was a bit lackluster while taking screenshots yesterday, I was attempting to solve the issue when I took the screenshots which is how some things became unhooked. I’ve replaced the image with the correct structure now.

Regardless, in the later screenshots within my post, the meshes produced by this function are being added and recognized by the blueprint’s instance editable array, which the Add to array had to be connected to the return value for those static meshes to show up. However, shortly after the meshes within the array are marked as trash and are removed soon after.

It turns out I can’t edit the original post, so here is the correct image:

its all good, no need to apologize. not exactly your fault. the forum lowers the resolution on upload. it’s very common.

if you’re adding the components to the array, they should persist. also it seems you’re calling attach to parent, that should set the owner on the component, iirc.

like i mentioned before, it depends on when you are creating those objects.

you need to make sure you create them on OnConstruction, or i think in bp would be Construct. make sure to test this properly. (on construction might be an override method)

you have to make sure you EMPTY the array, and the recreate the components.