Help with Dynamic Material

Hello, Everyone!

I’m having trouble with getting a material to change color dynamically.

I have a static mesh: SM_LaserMesh
I have a blueprint for this mesh: BP_LaserMesh

Both of these have no assigned material and are using the Unreal default.

The laser is created with BP_LaserCreator.

I am trying to replace Set Material with a Dynamic Material:

I want the laser color to change when I press the PageUp key. This what I have in the BP_ThirdPersonCharacter blueprint:

What I cannot figure out, is what to connect to Object in Cast To BP_LaserCreator. Any ideas as to what I am missing? Or am I just way off on how implement the dynamic material instance?

In order to cast to an object, you need to have a reference to a spawned actor. The function, “Get Actor of Class” would give you a reference, so long as BP_LaserCreator has been spawned during runtime. So, prior to casting to the class, you need to call the “Get Actor of Class” function again for the BP_LaserCreator class, then cast from that return value.

I’m also pretty sure if you were to call the event, “ChangeLaserColor_Event,” that function “Set Vector Parameter Value” won’t work because a valid reference isn’t being given (since it is pulling the return value from a function that may be out of scope). When you call “Create Dynamic Material Instance,” you should set it to a variable that then can be referenced in your event to set the param value.

OK, I’m not even sure that given the blueprint I have created that I can create a dynamic material instance. All of the examples I have seen show that a mesh needs to be set up in the Construction Graph. Since I don’t have anything to construct when the game starts, I don’t think I can use the Construction Graph because I can’t spawn a mesh in there.

This is the BP_LaserCreator:

Now I can put a Set Material function in between SpawnActor Static Mesh Actor and Set Cast Shadow, plug in Static Mesh Component, and make the laser any color I want. I could also put the Set Material function at the end, in between Set and Add, plug in LaserMesh (off of Set), and also set the laser color to anything I want. (I have done both of these successfully).

What I have not been able to figure out, is how to use Create Dynamic Material Instance instead of Set Material so that I can use a keyboard key event from the BP_ThirdPersonCharacter blueprint to change the laser color.

What I have tried to do (among dozens of other things), was to create BP_LaserMesh and spawn that in place of SpawnActor Static Mesh Actor so I could use use my mesh (SM_LaserMesh) in the Constuction Graph of BP_LaserMesh and hopefully be able to change the laser color dynamically.

In case you are wondering, I already have a material with parameters that I have applied to SM_LaserMesh, and have a test level where I put SM_LaserMesh in the BP_LaserMesh hierarchy and successfully change the mesh color from the BP_ThirdPersonCharacter blueprint with a keyboard key event.

So what I need help with is either implementing Create Dynamic Material Instance instead of Set Material or spawning BP_LaserMesh instead of SpawnActor Static Mesh Actor.

Alright, it’s not elegant or efficient, but I figured out a solution. Instead of spawning a Static Mesh Actor, I put the static mesh in a blueprint and spawned the blueprint. Then I created the dynamic material instance in the Construction graph of the blueprint.

(The laser meshes are just small segments arrayed together in BP_LaserCreator for the necessary laser length)

BP_LaserMesh (The blueprint containing the laser meshes):

BP_LaserCreator (The blueprint with the mesh model that spawns BP_LaserMesh)

BP_ThirdPersonCharacter

I only included the code to change the laser color based on a counter, not all of the code that executes all the other actions that take place for each color change.