Delete an actor c++

Hi.
I have an issue when I delete actor in c++. This actor has child actor component, but when I delete this actor child actor still exists, so I want to delete it in a function, so I have questions. When you delete an actor in the editor with delete key which function called? How can I delete these components? (I found ChildActorComponent->DestroyChildActorComponent and ChildActorComponent->DestroyComponent, but I don’t know if they are true)

try

	TArray<UChildActorComponent*> children;
	this->GetComponents(children);

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

(where this is the actor with the child componentes)

Thanks for the reply, I will try it if I can find the function which called when you delete an actor, but when you press the delete key which function calls?

Shouldn’t destroying the actor take care of the components too, including child actors?

I think so, but it doesn’t destroy the child actor components, or child actor class. I am new I am not sure which one is still there.

I checked it. It deletes the components, but when I add Actor to the world, it creates a lot of child actor component, and they are not connected to the parent actor. I am new, so sorry for misinformation.

Grid.h (2.6 KB)
GridMeshInst.h (666 Bytes)
GridVisual.h (981 Bytes)
GridMeshInst.cpp (1.4 KB)
GridVisual.cpp (2.3 KB)

Grid.cpp (9.3 KB)

At first glance try moving this to BeginPlay(), or maybe override virtual void PostInitializeComponents();:

if (GridVisual) SpawnGrid(GetActorLocation(), GridTileSize, GridTileCount, GridShape);

OnConstruction() doesnt not execute Destroy() so all child actors created will stay in the editor instance. I see you have DestroyTheGrid(); so I could be wrong, but then again you also use OnConstruction() on GridVisual so same issue could apply. At very least you could move everything out to a place where it’s not being executed so often.

PostInitializeComponents() will run after all components have been initialized when spawning / adding the class in editor.

One way I’ve found to mimic the desired function of OnConstruction() is to have the class listen to specific value changes by overriding PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent):

#if WITH_EDITOR
void AGrid::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
	Super::PostEditChangeProperty(PropertyChangedEvent);

	TArray<FString> Property = { TEXT("GridShape"), TEXT("GridCenterLocation"), TEXT("GridTileSize"), TEXT("GridTileCount")};

	FString PropertyChanged = PropertyChangedEvent.GetPropertyName().ToString();
	
	if (Property.Contains(PropertyChanged))
	{
		/* Your code here */
	}
}
#endif

You will have to manually manage the destruction of the child actor using ChildActor_GridVisual->DestroyChildActor(); unless your method is able to update with value changes.

Any suggestion about the above is more than welcome! So far this is the best way I’ve found to mimic blueprint like interaction in editor.


I would first try moving everything out of OneConstruction() to be sure that is your issue, then play with overriding PostEditChangeProperty().

Let me know if this helps!