Getting / Setting Material Parameter Values During Runtime

I currently have a system set up to change the value of a scalar parameter (opacity) on the first material index when certain conditions are met.



TArray<FMaterialParameterInfo> info;
TArray<FGuid> guids;
float retrieved_value;

GetMaterial(0)->GetAllScalarParameterInfo(info, guids);
GetMaterial(0)->GetScalarParameterValue(info[0], retrieved_value);

float change = 1.0f;
if (retrieved_value - change <= 0.20f)
{
SetScalarParameterValueOnMaterials("Opacity", 0.20f);
}


I want to avoid using dynamic material instances where possible, and this system doesn’t require using dynamic materials. However, it seems pretty wonky. Is there a better way of doing this?

You can’t change parameters on anything other than a Dynamic Material Instance at runtime - that’s what they’re there for.

That’s odd if that’s really the case, as this code works fine on a material that wasn’t created as a dynamic instance.

Only works on editor because in editor runtime your materials are not cooked yet.

On packaged builds you will not be able to reliably apply changes to root materials.

yeah exactly. There shouldn’t be any reason to not use a Dynamic Material really, creating them is super straightforward and you can apply one DMI to as many objects as you like.

Ahh that does makes sense, thanks for the info!