In this lesson, you will learn how to bypass the rigidity of static materials by creating Dynamic Material Instances (DMIs) to modify visual properties like color, glow, or transparency during gameplay.
The Problem with Static Materials
When you assign a Material to an Actor in the editor, that Material is shared across every instance of that object in the world. If you change a parameter on one, it changes on all of them. To gain individual control—like changing a lightbulb's color only when a player flips a switch—you must create a Dynamic Material Instance. This essentially creates a unique, runtime-only copy of the material that you can manipulate via Blueprints.
Implementing Dynamic Material Instances
The process requires two steps: initializing the instance and setting its parameters.
- Creation: In your Actor’s
BeginPlayevent, use the nodeCreate Dynamic Material Instance. Target the specific component (like aStaticMeshComponent) and select your Master Material. - Storage: Promote the return value of this node to a variable. This allows you to reference this specific instance later in your logic.
- Modification: Use the
Set Vector Parameter ValueorSet Scalar Parameter Valuenodes. Ensure the "Parameter Name" matches exactly what you defined in your Master Material graph (e.g., "EmissiveColor").
Example Logic:
// Within the Actor Blueprint
// 1. Initialize
DynamicMat = MeshComponent->CreateDynamicMaterialInstance(0, MasterMaterial);
// 2. Modify based on a gameplay event
DynamicMat->SetVectorParameterValue(FName("GlowColor"), FLinearColor::Red);
Why This Matters
By using parameters in your Master Material (via Scalar Parameter or Vector Parameter nodes), you decouple your visual logic from your gameplay code. Instead of swapping entire materials, you are simply updating values on the GPU. This is significantly more performant and allows for smooth transitions, such as fading an object's opacity or pulsing an emissive surface to indicate an interactive item.
Key Takeaway: Creating a Dynamic Material Instance allows you to manipulate specific material properties at runtime for individual actors without affecting the global material asset.