Creating all Instanced Static Meshes at once

Hi Everyone. So I’m trying to create a process that generates many (millions+) of instanced static meshes at once. I was doing this with AddInstance and a loop, but this was starting to take a really really long time. I then noticed this question:

If this guy is doing what I think he’s doing, this should generate all my meshes in one fell swoop. Problem is, I can’t get it to work. This is what I have:




UInstancedStaticMeshComponent* ISMComp = NewObject<UInstancedStaticMeshComponent>(MyActor);
ISMComp->RegisterComponent();
ISMComp->SetStaticMesh(MyMesh);

auto x = new FStaticMeshInstanceData(true, false);
x->AllocateInstances(total, true);

ISMComp->InitPerInstanceRenderData(false, x);


TArray<FInstancedStaticMeshInstanceData> smdata;

for (int i = 0; i < total; i++)
        auto NewInstanceData = new(smdata) FInstancedStaticMeshInstanceData();

ISMComp->PerInstanceSMData = smdata;

//  I guess I would need to set the data here?

ISMComp->PerInstanceRenderData->UpdateFromPreallocatedData(ISMComp, *x, true);
ISMComp->PerInstanceRenderData->InitResource();



I have two problems here. Firstly, even this doesn’t run; the first of the last two lines is giving me a “use of undefined type FPerInstanceRenderData”. Not sure why, but I’m guessing it’s a header file issue?

Secondly, I need to somehow allocate the data to x, and I’m not sure when this needs to happen, see comment above.

Thanks.