I’m trying to add a blendable material to my Player Controllers’ Camera, so that when the player takes damage I can affect the material instance and have it update.
However, I don’t seem to be able to actually add the Dynamic Material Instance (or any material, for that matter) to the Camera’s list of Blendables. Using a Material Parameter Collection is no-good for me in this case.
void ABZGame_PlayerController::PostInitializeComponents()
{
Super::PostInitializeComponents();
if (DamageBlendable)
{
DamageDIM = UMaterialInstanceDynamic::Create(DamageBlendable, this);
if (DamageDIM)
{
GetViewCamera()->PostProcessSettings.Blendables.AddUnique(DamageDIM);
}
}
}
void ABZGame_PlayerController::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
// @TODO - Rude Hax - fixme!
if (GetCamSpringArm()->GetAttachmentRootActor() != GetPawn())
{
AttachCameraToPawn(GetPawn());
}
if (bIsSimulatingDamage && AllowDamageSimulation && DamageDIM)
{
DamageFadeTime = FMath::FInterpConstantTo(DamageFadeTime, 0.f, DeltaSeconds, DamageFadeSpeed);
DamageDIM->SetScalarParameterValue(TEXT("DamageValue"), DamageFadeTime);
GEngine->AddOnScreenDebugMessage(-1, DeltaSeconds, FColor::Green, FString::Printf(TEXT("%f"), DamageFadeTime));
if (DamageFadeTime <= 0.f)
{
bIsSimulatingDamage = false;
DamageFadeTime = 0.f;
}
}
}
EDIT: Nevermind, the code works, I was just modifying the wrong scalar parameter name (of course). I’ll leave this here so anybody else searching for a way to do this can see it here.