Subscribe to event

Hi! I can get SWidget class, which has OnMouseEnter inside it, from within my custom UButton class:


       
 TSharedPtr<SWidget> SafeWidget = this->GetCachedWidget();
if (SafeWidget.IsValid())
{
SafeWidget->OnMouseEnter .... 
}


How can I subscribe to that OnMouseEvent? So that when it fires, my custom class will execute some function too? I’ve looked through delegates docs, but that OnMouseEnter inside SWidget is virtual void, and does not look like its delegate, though I still can not wrap my head around it, maybe it is.

Won’t be a delegate if it’s virtual void. I assume you just have to override OnMouseEnter() in your custom widget and do stuff here.

Yep, generally if you’re creating a custom widget with any non-trivial new functionality, you’ll need to create a custom Slate widget - in this case, deriving from SButton and overriding OnMouseEnter.

Awww, I see, so there is no way for me to derive from UButton, from SButton only. Hmmm, guess I’ll need to alter the source code of SButton then, because if I will not derive from UButton, I’ll lose all the UMG goodness

The idea is to put your functionality in a custom Slate widget, and then wrap it with a UMG class. So you could actually derive a class from UButton too, but it would do very little. You just need to override a couple of methods like RebuildWidget (to create your custom Slate widget) and SynchronizeProperties (to expose whatever custom attributes your widget has).

It does require writing a bit more boilerplate code than would be nice, but it’s not too much work once you get your head around it.

Thank you! I’ll try and do it

EDIT: WOW, that actually was easy. If everyone tries to do it ever again, simply you need to derive both from UButton, and SButton (in 2 seperate classes, course), and combine those inside custom class. That gives all the functionality now, YAI!