Efficiently spawn chess board mesh tiles: What is the "Unreal" way?

I have a GameBoard class which contains the variable board, a TArray of Tile pointers. A single Tile displays the mesh correctly but when I try to render multiple tiles via one board instance nothing is rendered . Here is an example of one way that I have tried

  1. Tile Constructor

    Mesh = CreateDefaultSubobject(TEXT(“Mesh”));
    Mesh->SetStaticMesh(LoadObject(nullptr, TILE_MESH_PATH.GetCharArray().GetData()));

  2. GameBoard BeginPlay()

     for (int row = 0; row < DIMENSIONS; ++row)  
     {  
     	for (int col = 0; col < DIMENSIONS; ++col)  
     	{  
     		ATile* tile = NewObject<ATile>();  
    
     		FVector offset = FVector(row * TILE_WIDTH, col * TILE_HEIGHT, 0.0f);  
    
     		tile->SetActorLocation(this->GetActorLocation() + offset);  
    
     		board.Push(tile);  
     	}  
     }  
    
  3. In the editor drag a Board (which has a scene node component) into the scene and hit play

I am reasonably comfortable with how to use C++ but trying to transition to Unreal

Hey

I believe you need to use SpawnActor here instead of NewObject.

ATile* tile = GetWorld()->SpawnActor<ATile>(GetClass(), this->GetActorLocation() + offset, FRotator::ZeroRotator);

Also, you might want to look into using a UInstancedStaticMeshComponent if you are going to place the same static mesh into the world multiple times.

Hope this helps :slight_smile:

Thank you. I had to change GetClass() to ATile::StaticClass() but it now works!