Download

On Clicked from within Button Widget (this.onClicked as Blueprint) UE5

Hi Guys,
I cant believe I am that stupid, but once again I struggle on s.th. supposedly easy. And I can’t believe no one ever stumbled upon s.th. like this. (A google- and a forums search didn’t help)
I am trying to build a Button Widget to be reused in my UIs.
Lets say this Button has two different appearance. E.g. It can show an on and an off image, depending on other magic. And it changes this image, depending on the linked whatever is tuned on or off.
From an UI widget, where this button is used, I would call the onClicked event on the Button and maybe call the toggle function inside this button. But, what if I want this button widget to react on its onClicked itself? Like it would be in C#:

this.OnClicked += doSomeMagic…

How would I do that?

I hope you understand what I am trying to archive.

Any help would really be appreciated.

Thanks in advance,

Greets, Sebastian

Edit:
Ahh, I forgot… My hope was:


But this doesn’t fire.

When placing a new node, you should have the option for ‘Bind Event to On Clicked’. This is the same node that an external source would use to bind to that event dispatcher.

You can also drag the dispatcher that you want from that UI into the graph and you’ll be presented with a menu, one of the options is ‘Bind’. This will place the same ‘Bind Event to On Clicked’ node.

1 Like

Hi, MagForceSeven

thank you for your answer!
this is what I tried to do, but:


…what calls this bind?
What am I doing wrong?

Since I derive from Button, there is no Event Construct and no BeginPlay in the ButtonBlueprint,
And I do not find anything like “AddedToWidget” or so.
Like: What is the “Constructor” node of an Button Actor?

Thank you in advance :slight_smile:

Oh yeah, I forgot that the built in widgets are sort of weird. I know I did it from native, but I don’t remember what function I was in. I can check when I get home tonight.

The alternative would be that instead of deriving directly from Button, you instead derive from UserWidget (which you’re normally expected to be deriving from) and you keep an instance of a button and bind to that during OnConstruct.

1 Like

yeah, they are xD
I am glad its not just me with my c# way of thinking :wink:

Yeah,
I feared that…
and that’s what I am doing right now for the sake of “I have no time for Buttons” xD
But I was hoping to find an more OOP approach

I would be very grateful :slight_smile:

Thank you!

It looks like my native derivation of Button just uses the default constructor to establish those bindings. That’s not to say that there isn’t a way to do it from blueprint, I’m just not seeing it.

I don’t know if that is a fair “criticism”. Even with OOP there is the distinction between things you are and things you have. With most widgets it generally makes more sense to build them up through composition to have other widgets. Based on the behavior you’re adding to your button it’s worth asking “Could this really be given to another client that is expecting a button?”. I’m not really sure that is true given the toggle nature you’re adding. But design is in the eye of the beholder a bit. Good luck!

1 Like

No, no, it wasn’t meant as criticism!
I just was expecting to use an derivate of Button, since this, for me, seamed to be the most logical.
But, yeah, you are right, sometimes I should be more of a behavior guy

Using Widgets in a more cascaded way (widgets containing widgets containing a button) and not just as a simple part of a hierarchical structure (widgets contain buttons containing a solution for toggling) is definitely something I can live with :stuck_out_tongue:
I really appreciate your advices, thank you so much for your help.

I hope you’ll have a great Christmas!

Best regards,

Sebastian

1 Like

You can attach it to your event construct and it will assign the event to the button. Then under the OnClicked event you can add in the logic. If you need to re-use the OnClicked event logic, make all the logic a custom event and attach it to the OnClicked event. If you are creating a widget button at runtime, make a reference for it and you can set variables or call events from the parent widget.

Hi Triv1

that was basically what I was trying to do, but

there is no Event Construct kind of thing in Blueprints, based on Button

and there seems to be no other possible way to get s.th. happening on creating an instance.
I guess this is based on the way unreal is handling UI Elements somewhere below UserWidget.

And that’s what I’ve tried to avoid. Because this forces me to bind my toggle everywhere where I need this kind of Button

a possible solution could be s.th. like:

Note, that the “ToggleButton” is based on UserWidget, storing a button.

the downside would be, since the button is wrapped into widget, changing any button parameters must be done via blueprint and cant just be edited via editor by changing the parameter of an instance somewhere in a widget (at least as far as I know)

I think I’ll stick with my initial approach, living with the fact I’ll have to call the init from the surrounding widget.

Best regards

Yeah, that is too bad. I’m not aware of anything for widgets that functions like the components that allow you to show that data directly in the properties (but I could be wrong). Depending on how many of those parameters you’re trying to modify, you could always add them as properties of the wrapping widget and push them to the button. Though without a construction script or OnConstruct you still need that init call.

Sorry I misunderstood that you were using a derived widget button instead of one created through user widget. Not sure if its worth it to create a derived button for multiple uses, not very familiar with it. The solution you presented is what I assumed to be best practice when working with widgets, that or using materials with parameters that get changed by the widget at runtime through curves or player interaction.