C++ Create a Hierarchical Instanced Static Mesh for procedural blocks

Hello Guys,

Project:

  • Procedurally-generated block-based game (yeah, like Minecraft)
  • Open Source

Experience:

  • 4 years C#/Java
  • 5 Months of C++
  • Only a few days of Unreal Engine
  • No Gaming Development

After quite a lot of googling and watching videos I didn’t find a lot of comprehensive C++ code regarding this topic. The HierarchicalInstancedStaticMesh(HISM) seems to be quite new and the Unreal Syntax looks a bit strange to me.

The code I have so far:

AChunk.h

UHierarchicalInstancedStaticMeshComponent* hismc;

AChunk.cpp

AChunk::AChunk()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	this->hismc = NewObject<UHierarchicalInstancedStaticMeshComponent>(this);
	this->hismc->RegisterComponent();
	this->hismc->SetStaticMesh(SM_Cube???);
	this->hismc->SetFlags(RF_Transactional);
	this->AddInstanceComponent(this->hismc);
}

This isn’t running code, only copied from other questions. I know so far, that there’s only one HISM per block-type needed and if I want to show more blocks I just call hismc->AddInstance(Vector). What I don’t know is, how to properly create an instance.

  • Is the shown code correct?
  • How do I get/instantiate a StaticMesh of a green(grass) block for the HISM?
  • Do HISM child instances have colliding meshes to walk on them?
  • What’s the relation between meshes and meshcomponents?
  • Do the HISM children need to be recreated each tick or only when the blocks change?
  • How do I find the block in my array, when my character looks and clicks on it?

A few sidenotes:

  • No blueprints, pure C++
  • A link to a tutorial on block and chunk generation. link:Here

I know this is a long question, but if you could answer only a few or point me in the right direction I would much appreciate it.

Hello dear reader,

I’ve gotten this to run with this code:

Constructor

this->HISMC = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("HierarchicalInstancedStaticMesh"));
	
	SetRootComponent(this->HISMC);

OnConstruction

void AChunk::OnConstruction(const FTransform & Transform) {

	// Register all the components
	RegisterAllComponents();

	//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Blocks created"));

	this->HISMC->ClearInstances();

	//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Instances cleared"));
	Blocky::MapCreator<EdgeLength> mapCreator;

	mapCreator.CreateLandscape(blocks);

	//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Blocks rendered"));


	for(size_t x = 0; x < EdgeLength; ++x) {
		for(size_t y = 0; y < EdgeLength; ++y) {
			for(size_t z = 0; z < EdgeLength; ++z) {

				switch(blocks[x][y][z]) {
					case 1:
						this->HISMC->AddInstance(FTransform(FVector(x * 200, z * 200, y * 200)));
						break;
				}

			}
		}
	}

}

And now I have a small little landscape of blocks. Runs fine and 120+fps at runtime.
I can walk on them with my Test character.

But the ultimate goal is to be able to remove blocks. I’m aware of the “RemoveInstance(s)” method, but how to get the index of the block when it gets hit? I’ve read some things about collision channels, but no idea how to set them or use them in code.

To delete instances you can create
FBox SelectorBox = FBox(...);
and ask HISMC for overlapping blocks
OverlappingBlocks = HISMC->GetInstancesOverlappingBox(SelectorBox)
Then you can remove them by
HISMC->RemoveInstances(OverlappingBlocks);

Cheers :slight_smile:
Chairos