Download

How to trigger event on widget from c++

I’ve run through one of Ramas tutorials on events from c++. I tested it on my extended character class. It called the event function and this in turn called the event in the blueprint, printing a message to screen. Simple I thought. Yet attempting to do the same thing with an event on an extended userWidget class is proving difficult. On my character class I’m toggling a menu that I’ve built between visibility states. The function fetches a reference to the widget and then I call the event function from that widget. I added an inbetween debug message inside the actual widget class that I call first to verify the pointer is valid, then that function calls its Event function. Still no in editor print to screen. What am I missing here?

This is what I have so far, but it does not fire the event.


//UKoreWidget.H
//Creating an event to trigger in the blueprint from c++ call
UFUNCTION(BlueprintImplementableEvent, Category = "KORE WIDGET")
void myCustomEvent();


//UKoreChar.cpp   Called when "tab" is key pressed
if (WidgetTemplate) 
	{
		AKoreCtrl* myPC = Cast<AKoreCtrl>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
		if (!WidgetInstance)
			WidgetInstance = CreateWidget<UKoreWidget>(GetWorld(), UKoreWidget::StaticClass());
		WidgetInstance->testVar += 15;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::Printf(TEXT("%s"), *FString::FromInt(WidgetInstance->testVar)));
		UKoreWidget::TestCPPCallToBP(WidgetInstance);
		if (!WidgetInstance->GetIsVisible())
		{
			FInputModeGameAndUI Mode;
			Mode.SetLockMouseToViewport(true);
			Mode.SetHideCursorDuringCapture(false);
			myPC->SetInputMode(Mode);
			WidgetInstance->AddToViewport(9999); // Z-order, this just makes it render on the very top.
			WidgetInstance->SetVisibility(ESlateVisibility::Visible);
			WidgetInstance->myCustomEvent();
		}
	}
}

Okay so as a work around I tried creating a delegate function that would broadcast from my character class to the widget class. I created a test function inside the widget class that would just print to screen when it fired. This delegate function did bind and it did print so I know it’s getting through to the widget. But, when I link the delegate function directly to the myCustomEvent() - which is the widget BlueprintImplementableEvent, it does not fire the event in the event graph within the widget

Can you be more specific?

So what exactly are you trying to achieve?

So you have a subclass of UUserWidget and inside that class there is a listener method test function which will print some message onto the screen when the event(delegate) inside the function MyCustomEvent defined in your subclass of Character broadcasts or gets fired. Right? And you are calling this MyCustomEvent function from blueprint?

Are you using dynamic multicast delegate?

If that’s the case then make sure that you bind the listener function(test function) to the event or delegate you want to listen to, before you broadcast it.

I use BlueprintNativeEvents (which I prefer) and they work fine.

Just make sure you implement this in the cpp:



void UMyWidget::MyCustomEvent_Implementation()
{}


Add a ‘Call To Parent’ function in BP to call the C++ code as well as run the BP Script.

Thanks for the reply, I solved this by changing this line



WidgetInstance = CreateWidget<UKoreWidget>(GetWorld(), UKoreWidget::StaticClass());

to this


WidgetInstance = CreateWidget<UKoreWidget>(GetWorld(), WidgetTemplate);

The problem was that I was using the static class version, instead of using the WidgetTemplate which is a



TSubclassOf<UKoreWidget> WidgetTemplate;

This meant it wasn’t firing the event on the correct version of UKoreWidget. So instead of going the long route and trying to use a delegate (which isn’t needed at all) I can now just call the event directly from my widget reference, because it is now actually the widget I’m selecting in the editor.

Yeah that make sense, the ::StaticClass() is basically “exactly what this class is and no more” - so not a BP version of that class for example. You could have lots of different BP versions of UKoreWidget - so you’ve got to tell the code which one specifically to use.

Does this function has a particular usage? Like as a substitute to this pointer or something?

static UClass * StaticClass()
Remarks:
Returns a UClass object representing this class at runtime