Hi Unreal Community!
For a piece of UI I’m making (I’ll call it “Widget1”), I want it to have 3 child widgets, all of which inherit from a “HUD_Button” class that has some built-in functionality I want. However, I also want each child to create a different menu when it is clicked. Is it possible to override the OnClicked, OnPressed, etc. functions of the button widget from each child widget, in Widget1? If not, is it possible to override a custom event? (which I could set up to run on the button event)
Quite possible, indeed.
Parent widget:
Child:
- top bit: child overriding parent’s onClicked.
- bottom bit: override and optionally call parent’s event (works for functions, too, ofc); or just call parent’s event / function (very bottom)
Ah, I understand what you mean, but I meant an instance of a source widget, as in a “Child” in the widget hierarchy of the Designer. I have a widget with 3 “child” widgets in the hierarchy, all marked as “is variable”.
Is there a way to override (in “Widget1”) a function that is defined on the source widget of the “child” in the hierarchy? e.g. I have a function called “ButtonPressed”, called when the button within the source widget is pressed, the behavior of which I want to customize for each instance of the source widget within “Widget1”. Thank you so much for your answer; I should have been clearer!
- if this is a user widget
- and this is the parent container hosting those widgets:
Set the instance’s data to have said instance take a different execution path. Or use a select node to feed other elements pertinent data. Here, providing each user widget was given a different ID, that widget would take a different execution path.
There are betters ways than using an int, ofc - enumerators are much more descriptive & human-readable, for example.
Another, slightly more advanced technique that does not require you to encapsulate the functionality within the user widget is to Dispatch the ID.
- the child dispatches its ID (here, an Instance Editable enum):
- only to have it caught by the parent:
Now you can create a bazillion of generic light-on-script widgets that do know what they’re executing; it’s decided by the underlying, owning widget instead. Only the parent needs to know about the functionality.
Optionally, at the bottom, one can manually set up dispatchers if so preferred.
Thank you so much! That isnt quite what I was hoping the answer would be, but It’s still very helpful and I’ll try it! Thank you again so much!
Perhaps consider rephrasing it one more time.
I have a function called
“ButtonPressed”, called when the
button within the source widget is
pressed, the behavior of which I want
to customize for each instance of the
source widget within “Widget1”. Thank
you so much for your answer;
For example, what does it mean to customise the behaviour? This sounds very much like what I originally posted. The base class has a function and the inheriting children override it with their own versions. Each child would be able to handle it differently providing they’re of different class.
Perhaps your have Interfaces on your mind, where unrelated classes can talk to each other via a set of functions declared in the Interface.
- the player interacts with various actors in the scene by pressing E
- press E on the tree, Shake the tree, apples fall
- press E on the medkit, Use the medkit, health goes up
- press E on the bucket, Kick it, we’re dead
Here Shake, Use & Kick are interface functions and the target object decides how to implement any of them. From the player’s perspective they just interact in the same way, but the end result depends on the target actor.