C++ Alter material during runtime

I am trying to ramp up the emissive brightness when the user presses a button (or moves a controller stick). I had it working with a combination of Dynamic material instance and timeline in the Blueprint editor, but I have found nothing that works in creating a C++ version.
And to make matters worse, if I put some bad code in the C++ file in Xcode and hit compile, it crashes the app and I have to remove the offending code, and trash the Build folder just to get the editor to reopen. >:(
I cannot figure out how to:
1… Access the material that is already set on the mesh.
(Should that material be the regular material or should it be the material instance?)
2… Change the two parameters I created in the material for different brightness floats.
3… Use a timeline or curve to ramp up the material property, then bring it back down when the button is release.

Any help is MUCH appreciated. (I have no code to show because I had to take out the mess I tried.)

Edit: If anyone knows of a working code sample, that would be fantastic.

Hm, just some ideas:

  1. Try FindObject ?
  2. You’ll need to use a material instance to dynamically change the material parameters: See class UMaterialInstanceDynamic (e.g. ENGINE_API void SetScalarParameterValue(FName ParameterName, float Value); to set a scalar parameter value in a material instance)
  3. Maybe do something like: OnClicked, OnReleased. And while it’s clicked (that is OnReleased hasn’t been called) use some FMath interpolation to interpolate your brightness variable.

Hope it helps a bit.

It seems my issue was in attempting to assign the UMaterialInstanceDynamic in the constructor. I moved that to an override of BeginPlay() and now it works fine. Now I just need to figure out utilizing a curve to gradually ramp up the brightness over a couple seconds.

InterpEaseInt and InterpEaseOut?

MyBrightness = FMath::InterpEaseIn<float>(MyBrightness, MyDesiredEndBrightness, DeltaTime, 2.0f)

I hope this is the correct way of using this function.

It’s good in theory, but I would need to move my change in brightness to Tick() and then find a way to have it easing in when the axis or button is engaged, then ease out after. That was easy to do in the Blueprint using a Timeline curve, but I am still foggy on how to do it in C++. :wink:

I don’t know the Timeline blueprint stuff : s.

There is UTimelineComponent.

You will have to hand in a function though that will handle the interpolation of the value. What exactly does one have to specify in the blueprint timeline to get interpolation to work?