Changing material color using buttons.

Hi everyone :slight_smile: I need your help.

I want to change the color parameter in material by clicking button on created interface. I have BP with interface (image 2) with few buttons which are created in other BP (I have two variables there: color of the image on it and slot number), BP with chair (image 1) and level BP where I call the event created in chair BP. I created dynamic material already. I can change and set (or randomize) color of chair but my goal is to get colors from buttons. Or collection, but it would be still depending on number of slot.

I’m trying to get this number (variable BigIntSlot) from BP with interface (called ColorInterface) and using this number want to get color from collection…but if I could get slot number then I would also get slot color.

Really hope for your help :slight_smile:

Hey, I think your problem is that you are currently creating a new interface at the beginning of the event. This means that all the variables within that newly created interface would be reset to default. Instead of creating a new widget, try casting to the one you already have to get the variable.

Something like this: (make sure to assign your current widget to the ColorInterface variable)

Hi, thank you for the answer!

It sounds so logical and now I realize how the CreateWidget works. Thanks! But now i get failed casting. Do you know why?

Hey again,

you still need to create the widget itself somewhere. I just assumed you already did that. If you did, then can you show me how you created it?

If not then have a look at the last section here titled ‘Displaying the HUD’ : https://docs.unrealengine.com/latest/INT/Engine/UMG/QuickStart/4/index.html
This shows you how to create the widget and assign it to a variable.

The way casting works is that you have to specify which particular object you want to cast to, so you have to create the widget somewhere and assign the created widget into a variable (just like they did in the example above). Once you created that variable you have to access it to cast to the widget itself.

In the example, they created the variable inside the character blueprint, so you would have to cast from your own blueprint to the character blueprint to get the variable and then use that to cast to that particular widget.

Here is an example from my own project. In the top I create the HUD inside the MyCharacter blueprint and in the bottom I cast to it and then cast to the HUD to set a variable inside the HUD itself. (You can use it the same way to get a variable as well)

I did create, but in level blueprint, so I moved it to MyCharacter BP. Now the casting is fine :slight_smile:

Tell me one more thing, please. In the picture you can see my interface - these are buttons created in other blueprint. Now I just need to get the values which I set manually (SmallIntColor and SmallIntSlot). When I try to do that I get the default zero values because they are zeros in blueprint with single button. Do I need function in ColorInterface with casting to InterfaceSlot or what?

Great thanks for help so far!

I want my level to know which button I clicked. I could do this like you wrote, but it should assign value which I indicate.

I created single button and it has variables with color and number. Then I set these colors and indexes (0 for first button, 1 for second and so on…). The problem is I can’t get these values from outside this single button’s blueprint. I can assign single button’s variable to some new one, but it assign default values which are always zeros (I set them then when I drag some buttons to my HUD).

I am not quite sure, since I didn’t use UMG that much yet, but I think you should just be able to drag the InterfaceSlot variable into the event graph and get the variable from there. You could then assign the value of that variable to BigIntSlot (if that’s what you’re trying to do.)

I still don’t quite get what you mean. Could you post some screenshots of what you have so far?

OK, so in details:

MyPlayer blueprint - adding interface and mouse cursor

InterfaceSlot - single button with image on it. SlotColot variable is binded to Color and Opacity to showing colors on buttons in game.

EventGraph of the InterfaceSlot - executing event from level blueprint.
Get Slot Params - honestly I don’t know does it have any sense, but when I was trying to print values locally in this blueprint it was working fine.

ColorInterface - interface added to the viewport. It containt 5 instances of the single button.
n the picture you can see variables created in InterfaceSlot. I can set these values and it looks properly in game. The event graph is empty, I can’t do some OnClicked event in this blueprint (already in previous BP). I was trying create some function but I couldn’t get values which I set. I was always getting zeros.

Here I just have to join these parts. I need an index of clicked button and then I can get colors from collection, but it won’t be necessary when I’ll get values from my interface.

I think that’s all. Hope it’s clear now. Thanks again for taking time for me!

Ahhh alright, gotcha. The easiest way that I can think of would be to cast to ColorInterface within the InterfaceSlot blueprint. So on your onClicked event, right BEFORE execute console command cast to the ColorInterface blueprint (just like you did in the ChangeColor blueprint. Then as ColorInterface set the BigIntSlot value to the same value as the SlotNumber (just use a get node by dragging it in from the side). Something like that:

Hope this solves it :slight_smile:

EDIT: Oh and yes, the function you created is useless. The variable will already use the default value that you manually put into it.

EDIT2: As a general bit of advice, if you ever have 2 blueprints working together, you will need to use casting to transfer variables between them. A bit more on that topic here: Direct Actor Communication Quick Start Guide | Unreal Engine Documentation

Great, it finally works! Thank you so much ! :slight_smile:
I have read these docs and some different also, I always read firstly, then ask for help if I still need, but this time it was horrible! I don’t like beginnings with new software… But know I start to understand casting and communication between BPs. Once again thanks!