Create and modify a dynamic material instance inside a a widget

I am an experienced coder but very new to blueprint scripting. Ive been doing several small projects in Unreal to teach myself but I’ve ran into a wall with one problem. I cannot for the life of me figure out how to create and modify a dynamic material instance inside a widget. I’ve read countless threads here but none seem to hit my problem on the head. I created a diablo-style health orb combining a couple threads here.

348880-dorb.gif

Everything works great and it’s very pretty.

But I cannot figure out how to put in in a widget and modify it. So I know in the contruct you create the dynamic material. Is this the construct of the widget bp? or the construction script in the character BP?

I stripped all my code that wasn’t working and added this pin just to ask for help here. For the “parent” of this do I select the original material instance which was then placed inside a widget? I’m not really sure where to go from there. Every article I read has a different answer. After that I know promote to variable etc. but nothing seems to work for me.

In the widget I know you are supposed to set a bind here. But everything after that is a little fuzzy for me. Where do I modify that brush? How do I modify it at runtime from the character BP? All the answers I’ve found here have been either vague or not lining up to what I have. Some say drag this node here or there but that node isn’t an option in my project, or they have a picture of a node tree with no context on where the variables come from or are generated. Any help would be greatly appreciated I am truly stuck here.

Well, to answer my own question…I got it working but in case anyone has the same issue I had I’ll post my solution. I’ll be as specific as I can since that was my issue getting it to work. I kept missing one step or variable that wasn’t explained in the posts I read.

So, first, in the widget blueprint set the bind for the image.

This will create and open up a new function in the widget. It will have 2 nodes: Get_FunctionName and Return Node. On the return node promote the return value to a variable, let’s call it “HBrush”.

Now in the widget Event Graph under the “Event Construct” we create our dynamic material instance. Pull a node from the construct and select “Create Dynamic Material Instance”. On the return value promote to a variable. I named mine Health_Orb_Dynamic. Next Ctrl+Drag the HBrush we created into the graph, pull a node from it and select “Set Brush Resource to Material” and connect it to the Health_Orb_Dynamic node.

Now go back to the designer in the widget and change the bind we created to HBrush.

Now back to the event graph of the widget, right click and add a custom event. Mine is called “Update_Health_Orb”. Now add the variables that need changed to it. For me its 3 variables. “Current_Health” (Float), “Max_Health” (Float), and “New_Color” (Linear Color). Now ctrl+drag the “Health_Orb_Dynamic” variable to the graph. Pull a node from it and use “Set Scalar Parameter” and connect it to the custom event. From there chain together all the variables that need edited like so:

Now we’re done with our widget don’t forget to compile. On to the main character blueprint. In either the construction or begin play event we add our widget. “Get Player Controller” > “Create Widget” (select your widget in class) promote the return value to a variable I called it “UIWidget” > “Add to Viewport”

Lastly to edit the widget, in the character blueprint to whatever event you want, I just did press F to demonstrate, we send the new values to the widget event. I have 3 values in my character blueprint named “Player Health”, “Player Health Max” and “Const Health Color”. We feed those into the custom event we made. First ctrl+drag the new variable we made last step called “UIWidget” to the graph, from it drag a node out and select the event “Update Health Orb” and send the variables to it.

348924-widget7.png

We’re all done. If you have questions or comments feel free.

3 Likes

Did you ever run into this while researching:

1 Like

Yeah that’s the initial article I based this on. I used my own art and created a bit of animation to spice it up. I got the material up and running I just ran into a snafu trying to get a dynamic material instance working. Basically either they didn’t say or I overlooked the step where you have to go back to your widget and change the brush to the one you create after setting the initial bind. But it’s all running well now, not really using it for anything other than learning.

1 Like

Thanks! This is exactly what I was looking for and it worked greatly. Your instructions are very clear.