# How to avoid repetitive code for static mesh and skeletal mesh components

**URL:** <https://forums.unrealengine.com/t/how-to-avoid-repetitive-code-for-static-mesh-and-skeletal-mesh-components/503498>\
**Category:** C++\
**Tags:** unreal-engine, UE4, Blueprint\
**Created:** [March 2, 2022, 7:38pm UTC](https://forums.unrealengine.com/t/how-to-avoid-repetitive-code-for-static-mesh-and-skeletal-mesh-components/503498 "2022-03-02T19:38:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sp7UE](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sp7ue/32/88709_2.png) [@sp7UE](https://forums.unrealengine.com/u/sp7UE)\
**Post date:** [March 2, 2022, 7:38pm UTC](https://forums.unrealengine.com/t/how-to-avoid-repetitive-code-for-static-mesh-and-skeletal-mesh-components/503498/1 "2022-03-02T19:38:05Z")

</div>

I have a c++ class that contains both static mesh and skeletal mesh, as components. And I have blueprint instances deriving from this class. These blueprint instances could be either static mesh or skeletal mesh  
My question is I am updating the material by writing same code for both static mesh and skeletal mesh which i think duplication, an object could be either static mesh or skeletal mesh when its spawned  
Is there a way we can better handle it by making use of coding templates or some other way to avoid writing the same code twice

```auto
for (UStaticMeshComponent* sm : StaticMeshComponents)
	{
		if (UMaterialInterface* DefaultMaterial = sm->GetMaterial(IndexOfMaterialToMakeDynamic))
		{
			DynamicMaterialInstance = UMaterialInstanceDynamic::Create(DefaultMaterial, this);
			sm->SetMaterial(IMaterialDynamic, DynamicMaterialInstance);
		}
	}

	for (USkeletalMeshComponent* sm : SkeletalMeshComponents)
	{
		if (UMaterialInterface* DefaultMaterial = sm->GetMaterial(IndexOfMaterialToMakeDynamic))
		{
			DynamicMaterialInstance = UMaterialInstanceDynamic::Create(DefaultMaterial, this);
			sm->SetMaterial(MaterialDynamic, DynamicMaterialInstance);
		}
	}

```

---

<div class="post-metadata">

**Author:** ![JaredTherriault](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jaredtherriault/32/1194499_2.png) [@JaredTherriault](https://forums.unrealengine.com/u/JaredTherriault)\
**Post date:** [March 2, 2022, 8:59pm UTC](https://forums.unrealengine.com/t/how-to-avoid-repetitive-code-for-static-mesh-and-skeletal-mesh-components/503498/2 "2022-03-02T20:59:59Z")

</div>

You could use UMeshComponent from which both UStaticMeshComponent and USkeletalMeshComponent derive.

```auto
TSet<UMeshComponent*> MeshComponents;

if (StaticMeshComponents.Num())
{
    for (UStaticMeshComponent* sm : StaticMeshComponents)
    {
        MeshComponents.Add(sm);
    }
}
else
{
    for (USkeletalMeshComponent* sm : SkeletalMeshComponents)
    {
        MeshComponents.Add(sm);
    }
}

for (UMeshComponent* mc : MeshComponents)
{
	if (UMaterialInterface* DefaultMaterial = mc->GetMaterial(IndexOfMaterialToMakeDynamic))
	{
		DynamicMaterialInstance = UMaterialInstanceDynamic::Create(DefaultMaterial, this);
		mc->SetMaterial(IMaterialDynamic, DynamicMaterialInstance);
	}
}

```

* * *

[Runtime DataTable](https://www.unrealengine.com/marketplace/en-US/product/4c7c4f8d523f4c90b2e26d23502c526c) : Import and export game data to and from CSV or Google Sheets while your game is running!

[GSheets Operator](https://www.unrealengine.com/marketplace/en-US/product/aa61e3df41fa46629d73ab59cacf9429) : Interface with Google Sheets, add new and delete old sheets, edit columns and rows, make in-game spreadsheets and more!

[easyCSV](https://www.unrealengine.com/marketplace/en-US/product/b25b855096e74d088d7553ccbe4072f2) : Read data from any CSV and put them in an easy-to-access string map!

[iTween](https://forums.unrealengine.com/t/open-beta-procedural-on-the-fly-animation-in-ue4-itween/10472) : Free, smooth, procedural object animation
