Cast to UUserWidget class from C++

The task is to initiate the custom Event in the widget to refresh some variables.

I created a C++ class derived from the UUserWidget

#pragma once

// Includes from Engine
#include "Blueprint/UserWidget.h"

#include "ContainerWidgetUpdate.generated.h"

UCLASS()
class MYGAME_API UContainerWidgetUpdate : public UUserWidget
{
	GENERATED_BODY()

public:

	/* Update container widget in case of adding of removing some items */
	UFUNCTION(BlueprintImplementableEvent, Category = "Container Widget Update Event")
	void UpdateWidget();
};

Widget BP is derived from this class.
I found this event and used it in the BP. No any issues.

Next step is initiate this event from others C++ classes.
I tried todo next

#include "UI/ContainerWidgetUpdate.h"

And somewhere in the functions

Cast<UContainerWidgetUpdate>(UpdateWidget()); // does not work

Cast does not work. Probably I need to declare UContainerWidgetUpdate();

 #pragma once

// Includes from Engine
#include "Blueprint/UserWidget.h"

#include "ContainerWidgetUpdate.generated.h"

UCLASS()
class MYGAME_API UContainerWidgetUpdate : public UUserWidget
{
	GENERATED_BODY()

	UContainerWidgetUpdate(); // it does not work!!! Is was added to ContainerWidgetUpdate.cpp also.

public:

	/* Update container widget in case of adding of removing some items */
	UFUNCTION(BlueprintImplementableEvent, Category = "Container Widget Update Event")
	void UpdateWidget();
};

The question is how to cast to the UUserWidget class from other C++ classes.
May be this is a wrong approach.

Approach was totally wrong.

How it should be:

  1. Widget Blueprint should be derived from standard UserWidget
  2. For updating data in the widget is better to use function in this blueprint.
  3. For initiation of this function and control all widgets there is UMG blueprint derived from custom C++ class
  4. Events should be in that C++ class (BlueprintImplementableEvent or BlueprintNativeEvent)
  5. Initialization of events through Player Controller using reliable client function with casting to UMG C++ class and calling the events.
  6. Player controller function can be cast from the others C++ classes.

Yes, the cast is incorrect.
This is the syntax:

Cast<"TargetClass">("ObjectToBeCasted");

In return you get a pointer which is either “nullptr” if cast failed or a pointer of “TargetClass”.

So, in your case:

UContainerWidgetUpdate* containerWidgetUpdateCasted = Cast<UContainerWidgetUpdate>();
if(containerWidgetUpdateCasted != nullptr)
{
    containerWidgetUpdateCasted.UpdateWidget();
}

Note: You should always check if cast has failed, otherwise it’s very likely you will get crashes.

Don’t forget to actually pass in the object to be cast.

 UContainerWidgetUpdate* containerWidgetUpdateCasted = Cast<UContainerWidgetUpdate>();

Should be:

 UContainerWidgetUpdate* containerWidgetUpdateCasted = Cast<UContainerWidgetUpdate>(WidgetPtr);

Where WidgetPtr is an existing pointer to the widget object you want to cast.