UMG image binding

We have set up a UMG widget for our HUD. On the HUD is an image widget. We wanted to change the image based on the weapon that is being used.

So we bound the Brush to a Blueprint function that returns a brush. The function is shown below

The problem we have is that image is never displayed. How do should we be dynamically changing the material for the image?

Hey, you can use materials to realize your purpose. Firstly, create a material with used with ui setting. Secondly, create a vector parameters, and name for it. Third, use material function that is uvremap_uvs(i can’t remember the name),. Fourth, set the image with material,which was created at the moment. Last ,in your function ,hook the image object, and call getdynamicmaterialsinsurance function, and then use the returned object to call setvectorparameter(like it) for changing the brush image.

Wish this can help you.

I think your code should work - are you certain the material is actually being changed?

Of course not. Getdynamicmaterialinstance return one dynamic material instance object, which was created with the material you set. You can use remap_uv material function to control material range not tiling. In other word, you can point at uvs of texture with that material you can set uvs by the vector parameter which was created in your material. so clearly. If you want to change brush image, maybe you should expand image widget.

Yeap, the material will be changed with this operation. I have show you the reason.

It never changes the material, which was saved into your disk.

Zql: I need to know how to pull a material from another actor. A single texture with all required images won’t cut it for some of the other tasks that require images as well. As an example - showing steam avatars.

Cgrebeld: I used a breakpoint on the return Value. In the brush it created I could see the name of the material assigned to the weapon. I’m pretty sure the material and brush are correct, it just doesn’t render them. I assume there is some other approach to achieve it?

Have create material with used with ui setting, and link the output pin of texture sample node to emissivecolor of material?also you do not use oqupa mode if the texture has alpha channal .

You can read the document of using umg guide.

In here i will upload two images for description how did i create the material and use it.alt text

Sorry but I still don’t understand how I do this without having one texture containing all the images. Can you explain how to use your approach without having every image contained in one material?

I read the UMG documentation before I posted here. I’m trying to figure out how to use the image as explained here

Image Displays the specified image. Currently this has to be a Slate Brush, however there are Blueprint functions on the Widget that allow you to set the image from a Texture or Material.

The last bit (that allow you to set the image from a Texture or Material) implies you can update the image

So, you want to set image or material without by control the range of one texture? if you just want to reset image while the weapon changed, there are at least two ways. You can use setbrushwithtexture or setbrushwithmaterial. You had better read image widget class for getting more detail information. Actually ,i suggest you control the range by setting uvs of texture.

The way I got it working in the end was similar to.

I added a material to the brush that takes a texture as a parameter. Same idea (material that you put values into), but instead of 1 material and moving the UVs about, I just change the texture in the material.

After that I created a subclass of the UserWidget called GameHUDWidget, and wrote some blueprint implementable events in C++. Then I was able to set the HUD Blueprint to receive those events and set the current weapon when the weapon change event happens.

First of all, this is almost 3 years old and already resolved. That’s one necrobump.

In you image, you SetBrush first and then Return no Value, setting the brush to default. This function expects you to return Brush data. Instead of SetBrush, just plug in the SlateBrush into ReturnValue. Do note that this is not a good approach as this will be executed every frame. If you want to just change material, use a CustomEvent.

Also look into SetMembersInStruct node - it will help you avoid that break/make spaghetti.

Could you maybe post a screenshot of your setup? I’ve been trying to get image replacement working so I can have a Parent Widget and then change the image with the children but it’s not working.

This is my Binding but even when I change the material, it’s blank in-game.

Everynone pretty much describes what I did. I had a blueprint event that set the material on weapon change, and I’m pretty sure he’s correctly identified your problem - Image brushes expect the image to be returned.

Thanks for the help Everyone. So that got it working, but now I’m concerned with the fact that you said it will run every frame. I didn’t realize that Binding the brush will run every frame. What’s causing that to happen? What if I just throw a Do Once node in there? That should prevent it from running every frame.

Also, I’ve added these widgets to another “HUD” widget and although it updates the images in game, inside the HUD, they all look like the default image. Is there any way to get them to update before you hit play?


Also sorry GeekyPayback, your solution was just too confusing to understand :stuck_out_tongue:

Any binding functions will execute every frame, that’s how things work in UE4. It’s not critical until you really care about the performance - things get serious when you have 100s of those running in loops. Then things pile up.

Not advocating for writing bad code here but I would not demonise it either, not in this very case - you’re just fetching an image (material?). Its impact on performance is negligible at best compared to some other things in blueprints that run whether you want them or not :slight_smile:

However, a slightly better coding practice would be to create a CustomEvent and switch the image when the time is right, just once. Alternatively, you can do it directly by SetBrushFromTexture (or whatsthename).

Even with DoOnce, it will still run every frame. When you return a value from a function, all the code inside goes out of scope. When the function is fired next time (frame) it does not know what happened in it previously so it will DoOnce again… To test it, put a Print String node inside and watch it spam.

The method @GeekyPayback suggested would also work nicely. Instead of switching materials/images around, you use a DynamicMaterial with a texture parameter and feed it data. Again, it all depends on the scope of your project, the way you set it up and what you expect it to do.

Perhaps you do not even need materials at all and your image is just called ‘material’ (which will confuse people! :D).

edit: have a look here for 3 different methods of handling property binding: