Instanced Static Mesh Component not rendering when instances are added through C++ [RESOLVED]

(Oops, made it a topic instead of a question)

After refactoring a bit of code to work with a few global Instanced Static Mesh Components instead of spawning/destroying static mesh components, i’ve run into an issue.

I can’t see my mesh instances!

Their collision is there, they’re in the Instances array and they’re being updated properly (I call UpdateInstance on them every frame to move them around), I can see them with Visibility Collision and Player Collision in the editor, but in game view, I cannot see them. Here’s how I add the Instanced mesh components (in BeginPlay):

InstancedItemMeshes.Reserve(Resources.Num());
for (const FResourceInfo& Res : Resources)
{
	UInstancedStaticMeshComponent* Comp = Cast<UInstancedStaticMeshComponent>(AddComponentByClass(UInstancedStaticMeshComponent::StaticClass(), false, FTransform(), false));
	Comp->SetMobility(EComponentMobility::Movable);
	Comp->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::SnapToTargetNotIncludingScale);
	Comp->SetStaticMesh(Res.BeltMesh);
	Comp->SetMaterial(0, Res.BeltMesh->GetMaterial(0));
	InstancedItemMeshes.Add(Comp);
}

Here, Res is a custom struct that holds some information I need.

And here’s how I add the instances:

IT.MeshInstanceId = IT.GetMeshComponent(GameState)->AddInstance(FTransform(), true);

And finally, here’s how I update them:

MeshComp->UpdateInstanceTransform(Item.MeshInstanceId, FTransform(NewWorldRot, NewWorldPos), true, true, true);
MeshComp->MarkRenderStateDirty();

Here Item is also a custom struct that holds some information relevant to the system this is apart of.

(See my previous post about this project for some more context, if needed.)

Hi - I saw your original post about using SMs first - I agree that ISMs are a more optimal solution and a singleton instance manager is the way to go.

Is it possible your materials don’t have the “use with instanced static meshes” usage flag set?

Oh! I forgot to mention that. Yes, they do have that check set.

(I even manually set the material of the component in the code, thought that may have been the issue, but no, it was not.)

EDIT: Perhaps it’s also worth mentioning that the Instanced Static Mesh Components are attached to and created by the GameState actor?

I’m not sure the GameState will work for containing those components - you could test by adding a simple mesh component to it as well to see if that renders.

I add a single actor to the level as my singleton - it’s in the world and in the actor arrays so is always in the correct render queues.

1 Like

Oh. Huh. That’s interesting, adding a static mesh component to the gamestate leads to the exact same behaviour. So I wasn’t doing anything wrong, I was just doing it in the wrong place.

I’ll try putting it on a seperate actor and I’ll mark as solution if that works. Thanks a ton!

EDIT: oh wait, right, i forgot to make it a question topic. I’ll rename it instead then. :joy:

1 Like