Simple InstancedMeshComponent example 4.92

Hey there,

Im currently trying to create a 3d hexgrid. Right now the only thing I got working is to have an actor with a bunch of staticMeshcomponents which all have a Hexagon model. The performance is awfull to say the least. So I did some googling and learned about Unreal’s “UInstancedStaticMeshComponent”.

I spend the last couple of hours trying to figure out how to use this component but no luck so far. I can find some people with similar problems in different topics, but the only solutions I found were meant for blueprints, not C++ coding.

So my questions, could someone please post a very simple example script on how you create an AActor with multiple UInstancedStaticMeshComponent? Or maybe point me in the right direction because I’m just stuck.

Edit:
I’ve managed to get the component working to such a point where I can see a list in the inspector of all the instances. However, not appears in the viewport.

AHexGridGenerator::AHexGridGenerator()
{
	PrimaryActorTick.bCanEverTick = true;

	meshComponentBase = NewObject<UInstancedStaticMeshComponent>(this, TEXT("Base Cell Mesh (Used to create instances)"));
	meshComponentBase->SetStaticMesh(GridInfo.StaticMesh);
	meshComponentBase->RegisterComponentWithWorld(GetWorld());
	meshComponentBase->AttachTo(RootComponent);
	AddOwnedComponent(meshComponentBase);

	for (int i = 0; i < 5; i++)
	{
		meshComponentBase->AddInstance(FTransform(FVector::ZeroVector));
	}

}

But when I look in the inspector and select the “meshcomponentbase” it shows that there’s no static mesh linked to the component. So for some reason

meshComponentBase->SetStaticMesh(GridInfo.StaticMesh);

doesn’t appear to work.GridInfo is a UStruct I created which I use to set the grid width, height and default model for a hex. This method worked perfectly when creating a bunch a ordinary staticMeshComponents so I don’t see why it should be a problem here.