Changing the Override Materials Array at Runtime?

Hi, I actually just have a small question regarding Override Materials.

The tooltip for “TArray<class UMaterialInterface*> OverrideMaterials;” says “Per-Component material overrides. These must NOT be set directly or a race condition can occur between GC and the rendering thread.

What does this mean exactly? We are changing the Override Materials array in many Editor Tools/Utilities, but we are also changing it at runtime in one of our custom AActor Class that spawns a Physics versions of the Actor when the player gets really close to it. In “OnConstruction”, we update the MeshComponent’s settings with the incoming Actor’s:

MeshComponent->SetStaticMesh(OtherActor->GetMeshComponent()->GetStaticMesh();
MeshComponent->OverrideMaterials = OtherActor->GetMeshComponent()->GetMaterials();
etc...

Could we run into GC issues doing this?

<

Hello,

Thank you for reaching out.

I’ve been assigned this issue, and we will be looking into these operations on MeshComponents for you.

Hello,

You should not set members in OverrideMaterials directly, even during OnConstruction.

The function you are looking for is “void UMeshComponent::SetMaterial(int32 ElementIndex, UMaterialInterface* Material)”. This will set the supplied Material into the specified index on OverrideMaterials, and ensure that the proper synchronization steps are followed.

Direct assignment to OverrideMaterials in the Editor’s GUI handles the synchronization during the property change. Setting the values directly in code does not handle this.

Please let us know if this helps.

Thank you, this is what I needed to know.

Hello,

Thank you for the reply.

Can we now close your case? You can always re-open it if you have additional questions about the same topic.