How can I change the values of a Dynamic Material in C++?

So I am trying to get a dynamic material to render properly but when I call SetScalarParameterValue, it has no effect on the material. How can I get the material to change based on the values that are passed in?

Here is the code that I have…

UCircualrUIDecalComponent::UCircualrUIDecalComponent(const class FObjectInitializer& PCIP) : Super(PCIP), ObjectFinderBaseMaterial(TEXT("/Game/UIStuff/NewUI/circUI.circUI")) 
	if (ObjectFinderBaseMaterial.Succeeded()) 
		DynamicMaterial = UMaterialInstanceDynamic::Create(ObjectFinderBaseMaterial.Object, this);
		SetMaterial(0, DynamicMaterial);

	DynamicMaterial->SetScalarParameterValue("health", health);
	DynamicMaterial->SetScalarParameterValue("armor", armor);
	DynamicMaterial->SetScalarParameterValue("XP", XP);
	DynamicMaterial->SetScalarParameterValue("passiveAbil", passAbil);

Hi DisastrousEvans,

I am a fairly inexperienced coder so prepare a grain of salt to go with my response.

I think it is unusual to set up an MID inside of the class constructor like that. I have never seen it done. I asked one of our rendering guys and he agreed; he suggested that any values there will not be initialized or will get stomped by serialization. Can you try creating the MID by some function that is triggered after postbeginplay?

After tooling around with this myself, this is the best solution I could come up with this problem.

I am new to Unreal and fairly new to C++ code, so take this advice as you wish:



    // Add the root to hold the bounds of this Annex 	
    AnnexBoundsMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Bounds Mesh Component")); 	
    AnnexBoundsMeshComponent->AttachParent = RootComponent; 

void ARootAnnexActor::OnConstruction(const FTransform &Transform) 
    // Set the DynamicMaterial so we can Edit it 	
    BoundsDynamicMaterial = AnnexBoundsMeshComponent->CreateDynamicMaterialInstance(0); 

In RootAnnexActor.h

// Add Dynamic Material
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = StaticMesh)
UMaterialInstanceDynamic* BoundsDynamicMaterial;

Later in Code I am change values with or even in OnConstruction if you really wanted to:

BoundsDynamicMaterial->SetVectorParameterValue(TEXT("BaseColor"), FLinearColor::Green);