OnConstruction executed too often

When i drag an instance of my actor in the level i get at least 3 duplicates. when i move the actor another duplicate is created. here is the code:

for (int y = 0; y < GridSizeY; y++)
		for (int x = 0; x < GridSizeX; x++)
		{
			TSubclassOf<ABlockBase> classname = ABlockBase::StaticClass();
			switch (Grid[y * GridSizeX + x])
			{
			case EBlockType::Basic:
				classname = ABlockBase::StaticClass();
				break;
			case EBlockType::Wall:
				classname = AWallBlock::StaticClass();
				break;
			case EBlockType::Empty:
				classname = AEmptyBlock::StaticClass();
				break;
			case EBlockType::Start:
				classname = AStartBlock::StaticClass();
				break;
			}

			UChildActorComponent* component = NewObject<UChildActorComponent>(this, FName(FString::Printf(TEXT("Test %i"), y * GridSizeX + x)), RF_Transient);
			component->SetupAttachment(scenecomponent);
			component->SetChildActorClass(classname);
			component->CreateChildActor();
			component->SetRelativeLocation(FVector(x * GridSize, y * GridSize, .0f));
         }
}

Uncheck “Run Construction Script On Drag”

1 Like

Ok that helps a bit. but when i move the actor it still leaves a “ghost” of all childactors behind.

If it’s re-running then you would need to add all spawned objects during your loop to a TArray then on consecutive construction iterate through it an if something exists delete it. That way it would clean up after itself.
OFC TArray would need to be declared in the header and not as a local variable.

I tried that, but it was not reliable. I am now using GetComponents(UChildActorComponent::StaticClass() …
and destroy all of them at the beginning of the construction script.
Also i seem to need to call registercomponent on all childactorcomponents, otherwise i get a strange outline on all of them.
but thank you for giving me the right direction.