UInstancedStaticMeshComponent - Moving Instances

I have a UInstancedStaticMeshComponent which I’ve added several Instances to, using AddInstance with an FTransform set to the initial position and rotation for each one. This works fine.

However I’m then wanting to be able to adjust the rotation of each Instance in code within the Tick function. Is there a way I can access each Instance by index so that I can do this? I’ve tried using GetChildComponent, but this doesn’t work presumably because the Instances are not actually components of the UInstancedStaticMeshComponent. I was also messing around with GetInstanceTransform, however this doesn’t work because the out parameter doesn’t point back to the actual Instance transform data.

Is this something that I should be able to do? I noticed that if I select the UInstancedStaticMeshComponent on the Actor while playing, I’m able to view the array of Instances in the editor and updating their Transform data in the editor is reflected in the Viewport. I just can’t find a way to do this in code.

int32 PetalInstanceCount = Petals->GetInstanceCount();
    
    FTransform PetalTransform;
for (int32 i = 0; i < PetalInstanceCount; i++) {
    
    	USceneComponent* Petal = Petals->GetChildComponent(i);
    
    		// This ensure fails because they're not really child components
    		if (!ensure(Petal)) return;
    		Petal->AddLocalRotation(FRotator(GetWorld()->DeltaTimeSeconds * 10,0,0));
    
    		// this doesn't work because I'm just updating an out parameter that has no link back to the Static Mesh
    
    		Petals->GetInstanceTransform(i, PetalTransform);
    		FRotator PetalRotator = PetalTransform.GetRotation().Rotator();
    		PetalTransform.SetRotation(FQuat(FVector::RightVector, PetalRotator.Pitch * 10 * GetWorld()->DeltaTimeSeconds));
    		PetalTransform.SetLocation(FVector::ZeroVector);
    
 }

You can use UpdateInstanceTransform

Petals->GetInstanceTransform(i, PetalTransform);
FRotator PetalRotator = PetalTransform.GetRotation().Rotator();
PetalTransform.SetRotation(FQuat(FVector::RightVector, PetalRotator.Pitch * 10 * GetWorld()->DeltaTimeSeconds));
PetalTransform.SetLocation(FVector::ZeroVector);
Petals->UpdateInstanceTranform(i, PetalTransform, false, false);

If you’re working in world space, make sure you set the third argument correctly (bWorldSpace).

The fourth argument (bMarkRenderStateDirty) should only be true for the last instance you are updating in a frame.

There’s a final boolean argument that flags movement type (bTeleport: true = teleport, false = affect velocity).

Thanks, that seems to be working!