Child actor component problem c++

Hi,
I’m having a very difficult time with child actor component. At the beginning, I was having problems with adding child actor component and casting it to use its variables. Finally I found a solution with this code:

void AGridVisual::OnConstruction(const FTransform& Transform)
{
if (!bIsInitialized)
{
ChildActor_GridMeshInst = NewObject(this);
ChildActor_GridMeshInst->SetupAttachment(GetRootComponent());
ChildActor_GridMeshInst->RegisterComponent();
ChildActor_GridMeshInst->SetChildActorClass(AGridMeshInst::StaticClass());
GridMeshInst = Cast(ChildActor_GridMeshInst->GetChildActor());
}

This code partially works. In the outliner panel below the BP_GridVisual blueprint, I can see the GridMeshInst, but there is nothing in the viewport. It is not important, because I don’t need it, but there are some other problems. For testing I am spawning 10x10 grid, in the onconstruction function. If I drag BP_GridVisual grids don’t spawn. If I close the editor, and recompile they spawns. Also, If I drag there is one GridMeshInst below the BP_GridVisual. After I recompile, there is two GridMeshInst. I don’t understand what am I doing wrong?
I will add all code below. Somethings is hardcoded because I am trying to make it work and then I will do the thing in another class:
GridMeshInst class:
include “Grids/GridComponents/GridMeshInst.h”
include “Components/InstancedStaticMeshComponent.h”

AGridMeshInst::AGridMeshInst()
{
PrimaryActorTick.bCanEverTick = false;
InstancedStaticMesh = CreateDefaultSubobject(TEXT(“InstancedStaticMeshComponent”));
InstancedStaticMesh->SetupAttachment(GetRootComponent());
}

void AGridMeshInst::BeginPlay()
{
Super::BeginPlay();

}

void AGridMeshInst::AddInstance(FTransform Transform, FIntPoint Index)
{

InstancedStaticMesh->AddInstance(Transform, true);
InstanceIndexes.Add(Index);

}

void AGridMeshInst::RemoveCurrentInstance(FIntPoint Index)
{
if (!InstanceIndexes.Contains(Index)) return;
InstancedStaticMesh->RemoveInstance(InstanceIndexes.Find(Index));
InstanceIndexes.Remove(Index);
}

void AGridMeshInst::ClearInstances()
{
InstancedStaticMesh->ClearInstances();
}

void AGridMeshInst::InitializeGridMeshInst(UStaticMesh* StaticMesh, UMaterialInterface* GridMaterial, ECollisionEnabled::Type Collision)
{
InstancedStaticMesh->SetStaticMesh(StaticMesh);
InstancedStaticMesh->SetMaterial(0, GridMaterial);
InstancedStaticMesh->SetCollisionEnabled(Collision);
InstancedStaticMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_GameTraceChannel2, ECollisionResponse::ECR_Block);
}

GridVisual class:

include “Grids/GridComponents/GridVisual.h”
include “Grids/GridComponents/GridMeshInst.h”

AGridVisual::AGridVisual()
{
PrimaryActorTick.bCanEverTick = false;
SceneComponent = CreateDefaultSubobject(TEXT(“SceneComponent”));
SetRootComponent(SceneComponent);
SetActorLocation(FVector(0.f, 0.f, OffsetFromGround));

/*ChildActor_GridMeshInst = CreateDefaultSubobject<UChildActorComponent>(TEXT("ChildActor_GridMeshInst"));
ChildActor_GridMeshInst->SetChildActorClass(AGridMeshInst::StaticClass());
ChildActor_GridMeshInst->RegisterComponent();
ChildActor_GridMeshInst->AttachToComponent(this->RootComponent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, true));
GridMeshInst = Cast<AGridMeshInst>(ChildActor_GridMeshInst);*/


//FTransform(FRotator(0.f, 0.f, 0.f), FVector(0.f, 0.f, 0.f), FVector(1.f, 1.f, 1.f));

/*SetRootComponent(SceneComponent);
ChildActor_GridMeshInst = CreateDefaultSubobject<UChildActorComponent>("ChildActor_GridMeshInst");
ChildActor_GridMeshInst->SetChildActorClass();
ChildActor_GridMeshInst->SetupAttachment(GetRootComponent());
ChildActor_GridMeshInst = NewObject<UChildActorComponent>(this);
ChildActor_GridMeshInst->bEditableWhenInherited = true;
ChildActor_GridMeshInst->RegisterComponent();
ChildActor_GridMeshInst->SetChildActorClass(AGridMeshInst::StaticClass());
ChildActor_GridMeshInst->CreateChildActor();*/

}

void AGridVisual::SetOffsetFromGround(float Offset)
{
OffsetFromGround = Offset;
SetActorLocation(FVector(GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z + Offset));
}

void AGridVisual::DestroyGridVisual()
{
GridMeshInst->ClearInstances();
}

void AGridVisual::UpdateTheVisual(FTileData TileData)
{
if (GridMeshInst)
{
GridMeshInst->RemoveCurrentInstance(TileData.Index);
if (TileData.TileType == ETileType::Normal) bIsWalkable = true;
else bIsWalkable = false;
if (bIsWalkable) GridMeshInst->AddInstance(TileData.Transform, TileData.Index);
}

}

void AGridVisual::InitializeGridMeshInst(UStaticMesh* StaticMesh, UMaterialInterface* GridMaterial, ECollisionEnabled::Type Collision)
{
if (GridMeshInst)
{
GridMeshInst->InitializeGridMeshInst(StaticMesh, GridMaterial, Collision);
}
}

void AGridVisual::BeginPlay()
{
Super::BeginPlay();
/UChildActorComponent ChildActorComponent = NewObject(this);
if (ChildActorComponent)
{
ChildActorComponent->SetupAttachment(GetRootComponent());
ChildActorComponent->RegisterComponent();
ChildActorComponent->SetChildActorClass(AGridVisual::StaticClass());
GridMeshInst = Cast(ChildActorComponent->GetChildActor());

}*/

}

void AGridVisual::OnConstruction(const FTransform& Transform)
{
if (!bIsInitialized) // default is false
{
ChildActor_GridMeshInst = NewObject(this);
ChildActor_GridMeshInst->SetupAttachment(GetRootComponent());
ChildActor_GridMeshInst->RegisterComponent();
ChildActor_GridMeshInst->SetChildActorClass(AGridMeshInst::StaticClass());
GridMeshInst = Cast(ChildActor_GridMeshInst->GetChildActor());

	if (DataTable)
	{
		FGridInfo* CurrentRow = DataTable->FindRow<FGridInfo>(FName("Square"), "");
		UStaticMesh* SquareStaticMesh = CurrentRow->Mesh;
		UMaterialInterface* SquareGridMaterial = CurrentRow->MeshMaterial;
		GridMeshInst->InitializeGridMeshInst(SquareStaticMesh, SquareGridMaterial, ECollisionEnabled::QueryOnly);
	}

	bIsInitialized = true;
}

	

FTransform(FRotator(0.f, 0.f, 0.f), FVector(0.f, 0.f, 0.f), FVector(1.f, 1.f, 1.f));
for (size_t i = 0; i < 10; i++)
{
	for (size_t k = 0; k < 10; k++)
	{
		FIntPoint IntPoint;
		IntPoint.X = i;
		IntPoint.Y = k;
		FTileData TileData;
		TileData.Index = IntPoint;
		TileData.TileType = ETileType::Normal;
		TileData.Transform = FTransform(FRotator(0.f, 0.f, 0.f), FVector(100.f * i, 100.f * k, 0.f), FVector(1.f, 1.f, 1.f));
		UpdateTheVisual(TileData);
	}
}

}