# Setting Dynamic Material Instance for Skeletal/Static meshes using C++?

**URL:** <https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE4, question, CPP\
**Created:** [May 8, 2022, 4:34pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079 "2022-05-08T16:34:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 4:34pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/1 "2022-05-08T16:34:16Z")

</div>

I am struggling with C++ Programming, I need a book or a course which will cover everything to teach me, so I can make my dream games such as fortnite, freefire, super people, pubg.

Thank You so much in advance.

---

<div class="post-metadata">

**Author:** ![Online\_Learner\_XWYN1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Online\_Learner\_XWYN1](https://forums.unrealengine.com/u/Online_Learner_XWYN1)\
**Post date:** [May 8, 2022, 5:39pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/2 "2022-05-08T17:39:17Z")

</div>

this will help

> **[Tag: Programming | Unreal Engine Community Wiki](https://unrealcommunity.wiki/browse/tag/Programming)**
>
> Displaying pages with the tag "Programming".

and setup the ide will made u start codeing easyer

> **[Choosing & Preparing Integrated Development Environment for Unreal C++ |...](https://unrealcommunity.wiki/setting-up-visual-studio-for-unreal-engine-hgkklk2i)**
>
> What do you need to efficiently work with Unreal C++?

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 5:46pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/3 "2022-05-08T17:46:29Z")

</div>

I am done with these things already and have some experience in C++, but when it comes to converting my blueprints to C++ , I have lots of problems to understand the limited introduction of the functions even in the source code and in the original documentation …  
I would like to see an official online bp script translation to C++ tool along with the documentation , its easy for UE Developers to make one but I am confused why they don’t have one, its already decades since the engine released and still we are struggling to convert even small nodes to C++.

---

<div class="post-metadata">

**Author:** ![Online\_Learner\_XWYN1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Online\_Learner\_XWYN1](https://forums.unrealengine.com/u/Online_Learner_XWYN1)\
**Post date:** [May 8, 2022, 5:53pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/4 "2022-05-08T17:53:30Z")

</div>

i`m here too, 🤣 ha ha, i always need google every bp function how to use in cpp

get open source version maybe help,u can double click bp function to check cpp code,but build is so slow

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 5:59pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/5 "2022-05-08T17:59:54Z")

</div>

I have the source build v4.20 and 27.  
I have just find a website to create Blueprint Nodes, its like a small part of he engine online, [https://blueprintue.com](https://blueprintue.com) and this is possible to create a tool in which you create nodes and on the other side you get C++ translation, I Hope UE Developers will think about it. This is something will help everyone to learn the engine quickly.

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [May 8, 2022, 6:20pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/6 "2022-05-08T18:20:44Z")

</div>

As someone who just started to dive into the engines code: everything got exponentially easier when I understood how useful searching for definition is.

E. G.: My time googling for function parameters has been reduced to 2 clicks and copy / paste.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 6:37pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/7 "2022-05-08T18:37:50Z")

</div>

but in most cases the 2 clicks and copy / paste will not work… according to my knowledge.  
If I am wrong please correct me. I can give you an example setting dynamic material instance in C++, many peoples take it it wrong and they are passing the float values to set parameters… this is wrong because we have the parameters already set up under the material and we want to override one material to the original material in the same slot.

I am struggling to convert this to working C++ code, already 3 days and not working.

 ![image_2022-05-08_233158654](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/e/2/ee24e98706628c7404fceb5c66bc150f800b483d.png)

What I am trying is:  
.h

```auto
class UMaterialInterface* MaterialSource;
class UMaterialInstanceDynamic* ReturnDynamicMaterial;

```

.cpp

```auto
void AMyCharacter::SetMaterialInstance()
{
	UMaterialInstanceDynamic* DynMaterialRefference;
	ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource, this, FName("MyDynMat"));
	static ConstructorHelpers::FObjectFinder<UMaterialInterface>MaterialSource(TEXT("/Game/Assets/MyDynMat"));
	if (MaterialSource.Object != nullptr)
	{
		 DynMaterialRefference = ReturnDynamicMaterial ;
		GetMesh()->SetMaterialByName("TopBody.Slot", LocalVariableMaterialRef);
	}
}

```

The Game compiles, everything is fine… but the material is not giving any result… not effecting anything.

---

<div class="post-metadata">

**Author:** ![mnelenpridumivat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mnelenpridumivat/32/1194643_2.png) [@mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat)\
**Post date:** [May 8, 2022, 8:18pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/8 "2022-05-08T20:18:58Z")

</div>

> [@Alexa.Ki](#):
>
> but the material is not giving any result… not effecting anything.

Is `LocalVariableMaterialRef` contains material to set? You created material, saved it to `ReturnDynamicMaterial`, then copy pointer to material to `DynMaterialRefference` (I think you can delete variable `DynMaterialRefference` because you have already saved pointer in class member variable `ReturnDynamicMaterial`, and `DynMaterialRefference` is not used in your code). But you don’t copy material to `LocalVariableMaterialRef`, so, if you haven’t set this variable earlier (in another function, for example), it will contains `nullptr`.  
Also (I have never set C++ variable using `ConstructorHelpers::FObjectFinder`, so, if I made a mistake, correct me please), I think another problem is you use variable `MaterialSource` before you set some value using `ConstructorHelpers::FObjectFinder`.  
To sum up, I’ll try fix your function code comparing to what I said. Try this:

```auto
void AMyCharacter::SetMaterialInstance()
{
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialSource(TEXT("/Game/Assets/MyDynMat"));
	ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource.Object, this, FName("MyDynMat"));
	if (MaterialSource.Object != nullptr)
	{
		GetMesh()->SetMaterialByName("TopBody.Slot", ReturnDynamicMaterial);
	}
}

```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 8:26pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/9 "2022-05-08T20:26:06Z")

</div>

> [@mnelenpridumivat](#):
>
> ```auto
> static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialSource(TEXT("/Game/Assets/MyDynMat"));
> ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource, this, FName("MyDynMat"));
> if (MaterialSource.Object != nullptr)
> {
> GetMesh()->SetMaterialByName("TopBody.Slot", ReturnDynamicMaterial);
> }
> 
> ```

Thank You for Helping Sir, but still the issue is not fixed.  
Now I got this error.

# [No instance of overloaded function matches argument list.]

![image_2022-05-09_013056480](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/4/5/345f7717751b16433b30367ea7175f220262f69e.png)

---

<div class="post-metadata">

**Author:** ![mnelenpridumivat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mnelenpridumivat/32/1194643_2.png) [@mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat)\
**Post date:** [May 8, 2022, 8:32pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/10 "2022-05-08T20:32:28Z")

</div>

Invalid parameter. It means you’re trying to pass `ConstructorHelpers::FObjectFinder<UMaterialInterface>` instead of `UMaterialInterface *`. I think `UMaterialInterface *` presents in `FObjectFinder` as a member of struct. Try `UMaterialInstanceDynamic::Create(MaterialSource.Object, this, FName("MyDynMat"));`. I fixed this mistake in my previous reply.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 8:40pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/11 "2022-05-08T20:40:29Z")

</div>

Now the engine is crashing on click to Play.

```auto
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialSource(TEXT("/Game/Assets/my dyn material"));
	//ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource, this, FName("MyDynMat"));
	UMaterialInstanceDynamic::Create(MaterialSource.Object, this, FName("MyDynMat"));
	if (MaterialSource.Object == nullptr) return;
	{
		GetMesh()->SetMaterialByName("TopBody.Slot", MaterialSource.Object);
	}

```

---

<div class="post-metadata">

**Author:** ![mnelenpridumivat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mnelenpridumivat/32/1194643_2.png) [@mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat)\
**Post date:** [May 8, 2022, 8:45pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/12 "2022-05-08T20:45:30Z")

</div>

What the error is? In which place of this code?  
But, why did you changed folder to `"/Game/Assets/my dyn material"`? Maybe because of this? It was `"/Game/Assets/MyDynMat"`.  
Also, now in `UMaterialInstanceDynamic::Create(MaterialSource.Object, this, FName("MyDynMat"));` you don’t save your new dynamic material. Delete this line and uncomment and change it to `ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource.Object, this, FName("MyDynMat"));` and change `GetMesh()->SetMaterialByName("TopBody.Slot", MaterialSource.Object);` to `GetMesh()->SetMaterialByName("TopBody.Slot", ReturnDynamicMaterial);`.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 8:49pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/13 "2022-05-08T20:49:19Z")

</div>

![image_2022-05-09_014846873](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/f/2/1f2cfe8eec1aa7ea779e693e6aeffd325c2c25bb.png)

this is the actual directory of the Material, I changed here just to make it short

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 8:57pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/14 "2022-05-08T20:57:00Z")

</div>

In my version of engine, it looks like this under UkismetLibrary.cpp

```auto
class UMaterialInstanceDynamic* UKismetMaterialLibrary::CreateDynamicMaterialInstance(UObject* WorldContextObject, class UMaterialInterface* Parent)
{
	UMaterialInstanceDynamic* NewMID = NULL;

	if(Parent != NULL)
	{
		NewMID = UMaterialInstanceDynamic::Create(Parent, WorldContextObject);
		if (WorldContextObject == NULL)
		{
			NewMID->SetFlags(RF_Transient);
		}
	}

	return NewMID;
}

```

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [May 8, 2022, 9:09pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/15 "2022-05-08T21:09:45Z")

</div>

> [@Alexa.Ki](#):
>
> `NewMID = UMaterialInstanceDynamic::Create(Parent, WorldContextObject);`

This function uses UKismetMaterialLibrary::CreateDynamicMaterialInstance. So rather than including another class, just use it directly like @mnelenpridumivat did:

> [@mnelenpridumivat](#):
>
> ```auto
> void AMyCharacter::SetMaterialInstance()
> {
> static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialSource(TEXT("/Game/Assets/MyDynMat"));
> ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource.Object, this, FName("MyDynMat"));
> if (MaterialSource.Object != nullptr)
> {
> GetMesh()->SetMaterialByName("TopBody.Slot", ReturnDynamicMaterial);
> }
> }
> 
> ```

Like I said, this was the hardest and most important step for me:

> [@pezzott1](#):
>
> My time googling for function parameters has been reduced to 2 clicks and copy / paste.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 9:11pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/16 "2022-05-08T21:11:29Z")

</div>

Now When It return a break point on compilation on 71% and crashed engine.

```auto
static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialSource(TEXT("/Game/Assets/Characters/FCharacter/Char_01/DynMaterials/DM_FH_01"));
	ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource.Object, this, FName("DM_FH_01"));
	if (MaterialSource.Object == nullptr) return;
	{
		GetMesh()->SetMaterialByName("F_Head_01_A", ReturnDynamicMaterial);
	}

```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 9:16pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/17 "2022-05-08T21:16:32Z")

</div>

Still not working, engine crashed while loading at 73%

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 9:19pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/18 "2022-05-08T21:19:24Z")

</div>

![image_2022-05-09_021921460](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/f/1/2f1e547f8ba0556eb934cfba0dd97b9d58877572.png)

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [May 8, 2022, 9:21pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/19 "2022-05-08T21:21:18Z")

</div>

Try this:

```auto
if (MaterialSource.Succeeded())
{
	int MatIndex = GetMesh()->GetMaterialIndex("F_Head_01_A");
	GetMesh()->SetMaterial(MatIndex, ReturnDynamicMaterial);
}

```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 9:25pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/20 "2022-05-08T21:25:49Z")

</div>

same crash and the same breakpoint.

[Next page](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079.md?page=2)
