Can't save MaterialInstanceDynamic created via C++

Can’t save D:/Documents/Programming/Unreal/DMI_Bug/Content/MyMap.umap: Graph is linked to private object(s) in an external package.
External Object(s):
MaterialInstanceDynamic_0

Try to find the chain of references to that object (may take some time)?

I’ve reproduced the bug in a standalone project and attached it here.
To get the message, simply create an instance of MyActor (a C++ class) and then save.

link text

unfortunately that doesn’t change anything. I had thought the same thing, is there a way of creating a material instance (instead of a material instance dynamic)

another thing to note is that if I make a dynamic material instance inside the blueprint construction script, everything works as I would expect it to

// the updated code that I tested with

AMyActor::AMyActor()
{
	static ConstructorHelpers::FObjectFinder<UMaterial> MyMaterial(TEXT("'/Game/MyMaterial'"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> MyMesh(TEXT("'/Game/MyMesh'"));

	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
	StaticMeshComponent->SetupAttachment(RootComponent);
	if (MyMesh.Succeeded())
	{
		StaticMeshComponent->SetStaticMesh(MyMesh.Object);
	}
	if (MyMaterial.Succeeded())
	{
		MID_MyMaterial = UMaterialInstanceDynamic::Create(MyMaterial.Object, StaticMeshComponent);
	}
}

void AMyActor::PostInitializeComponents()
{
	Super::PostInitializeComponents();
	StaticMeshComponent->SetMaterial(0, MID_MyMaterial);
}

quick update, it looks like by changing the OUTER of UMaterialInstanceDynamic::Create gets this to work
changed from MeshComponent → NULL

edit:
this is a bug, not a question, not resolved

I’m pretty sure it works and i just tested it again, maybe you had a corrupted blueprint? Deleting it and recreating it will probably work.
As for the answer you posted, it works because you’re not giving the material instance any thing to be a part of, so it won’t save. it’s just like setting its flag to Transient.
To test what i mean, set the outer to NULL like you said, and set the material in the costructor like you did before, without the PostInitializeComponents. And once you restart the editor the mesh will lose the material instance, because it didn’t get saved.

I can make a blueprint instance of it (in the project), but if I drag and drop it into the world, it still won’t save

Okay, after trying a couple of things i think this is the best way to go. As i said earlier, dynamic material instances are made to be created and edited during runtime so i suggest creating a constant material instance based on the material you wanna edit and using that in your constructor instead of the dynamic one, which will save just fine.

And if you want to edit it at runtime, just create a dynamic one based off of it and change its parametes, here’s the code i set up.

.h

282216-5.png

.cpp

Here’s my in-editor setup with a material instance constant set to a red value, level and actor both save fine because the instance isn’t dynamic.

At runtime, a dynamic one is created and set to the mesh.

thanks for the help dude, I’ll give it a go tonight when I get home

alright yeah that does indeed work better, but it seems to have the same outcome as making a dynamic that has no outer. I guess there isn’t anyway around having to store the material settings (not the worst thing).

I might look into creating a material instance with the params I want and saving it to the content browser (probably not though)

thanks for all the help! I assume that material instance dynamic settings are never meant to be saved ?

No problem! You can save the instance settings some other way like you said but the dynamic instance still has to be created and edited during gameplay.