Actor' OnConstruction runs twice

I am trying to create a grid for a grid based game, there is a grid containter, that contains the grid tiles.
I’d like to modify the grid tiles in the editor, so I wrote the code that creates the tiles and places them in the OnConstruction function.

Here is the grid container cpp:
void AGrid_Container::OnConstruction(const FTransform & Transform) {

	Super::OnConstruction(Transform);

	width = length = height = 4; //test

								 //Fill Grid --> tile[height][width][length]
	tile = new AGrid_Tile***[height];
	for (int i = 0; i < height; i++) {
		tile[i] = new AGrid_Tile**[width];
		for (int j = 0; j < width; j++) {
			tile[i][j] = new AGrid_Tile*[length];
			for (int k = 0; k < length; k++)
				tile[i][j][k] = (AGrid_Tile*)GEditor->AddActor(GetWorld()->GetCurrentLevel(),
					AGrid_Tile::StaticClass(),
					FTransform(FVector(0))
					);
		}
	}

	grid_tile_height = grid_tile_width = grid_tile_length = grid_tile_gap = 100.0; //default grid specs.
	for (int i = 0; i < height; i++)
		for (int j = 0; j < width; j++)
			for (int k = 0; k < length; k++) {
				tile[i][j][k]->SetActorLocation(FVector(j * grid_tile_width, i * grid_tile_length, (k * grid_tile_height) - 100.0));

			}

}

It works and all, but when I place the Grid_Container actor in the editor, it spawns each tile twice, on top of one another. My guess is that it runs twice for some reason, is there any way to avoid this?

Construction script runs every time the object is spawned in-game, created in the editor or one of the actor’s values is modified in the editor. Hell, it even runs when you drag the object in the editor. (although there is a flag to disable the last one)

I would save the size of the grid after creation and every time OnConstuction() is called I will check if the size has changed.

Now you will end up with a different problem: When the size does change you would have to manually destroy all created tiles and arrays and build the new grid from scratch. This could be fine if the grid is small and you don’t intend to mess with the size a lot or (destroy it) but otherwise I would suggest you use Unreal’s dynamic TArray. (In general, I would avoid manually allocating memory (with “new”) within a system with a built-in garbage collector)

P.S.

Also, why do you need a grid of Actors? Large grids would really slow your game down. If you use it only to snap objects to it you can just re-adjust the object’s position in their construction scripts by calling a single GridActor that will return the proper coordinates for placement. If you need every grid to have different color you would just build the texture and apply it to the whole mesh.

The editor spawns you actor twice. Once on drag to create a preview actor, and then again (the actual actor) when you release the mouse. Based on my humble knowledge there is no way arround this without a huge engine mod for a very minor thing. There is however a workarround. Put everything you don’t want for giggles on the PreviewActor in an “if”.
{ MyGiggles(); if (!HasAnyFlags(RF_Transient)) { MySh…tuff(); } } in the OnConstruction(Ftransform& … stuff) that is,… on your actor.