Download

UserWidget with C++ Base and events

Hello everyone,

I’m starting my foray into Widgets with C++. I have a new UserWidget created with a simple Text widget that I have bound to my base class. This works very well as I can set the bound variable in my base and instantly see the binding in the widget.

The problems begin when I want to bind something like an Image. It appears that the Image widget is not directly bindable. So, in my base class I created a private UTexture2D property. I also created a public GetThumbnail() function that returns this property. In my UserWidget, I bound the Brush of the Image to a new Function called RetrieveThumbnail(). Inside of RetrieveThumbnial() I call the GetThumbnail() function from my base.

This all appears to be working fine, but how can I fire an event (or some other notification) so that my UserWidget knows that the image has changed?

For testing purposes to make sure that everything was working, I plugged in my RetrieveThumbnail() function to my Tick() in my UserWidget - and it works fine. Obviously this is not optimal and need a way to trigger an event when the image in the widget changes so I can update the image.

Any ideas?

Thank you.

Let me understand better your problem, you have an UserWidget derived C++ class where you have a private UTexture2D UPROPERTY.
I’m assuming you created an UMG blueprint widget derived from this native C++ class that contains an UImage UMG widget in its WidgetTree.

So you have other BP instances that can access the brush of the image of this UMG widget and you want to know to trigger some event/function of the base C++ class when the brush is changed in order to modify the private UTexture2D property. Is that correct?

Actually it’s a lot more simple than that. You are correct in that I have a UserWidget derived from my base class. However, no other BPs are being used. I want to modify properties in my base from other code (I have a pointer to the instance of my UserWidget in my Character) and somehow notify the derived UserWidget that something has changed so it can do what it needs to graphically. Maybe this is the wrong pattern to use? Thanks for the reply.

Add a public function to the widget like “OnImageChanged()” and call it from the code that modifies the image?

Ok, so you can do this.
Create a public setter function for that property and define another function called OnThumbnailChanged with BlueprintImplementableEvent meta inside the UFUNCTION macro.
You don’t have to implement that function in C++ but it will show up as an event in your Blueprint derived class.

Header file



UFUNCTION(BlueprintImplementableEvent)
void OnThumbnailChanged();

void SetThumbnailImage(UTexture2D* NewImage);


Implementation file



void UYourUserWidget::SetThumbnailImage(UTexture2D* NewImage)
{
    YourImageProperty = NewImage;
    OnThumbnailChanged();
}


Hopefully I got what you are trying to do and this can for the most part be done in just code (as long as you make the UImage in the blueprint).



UPROPERTY(meta = (BindWidget))
UImage* MyImage;

UFUNCTION()
void UpdateImage(UTexture2D* NewTexture);




void UYourWidget::UpdateImage(UTexture2D* NewTexture)
{
    MyImage->SetBrushFromTexture(NewTexture);
}


You should be able to call the update image function from within the character (if the function is public) with an image, an this should change the visibile one.

Thank you. This works well.