How do I reference a class function in Widget blueprint?

I have a very simple function I want to call from C++ class called CameraPawn in the Widget Blueprint. Here is the C++ .h and .cpp files


   void Test123();


void ACameraPawn::Test123()
     GEngine->AddOnScreenDebugMessage(-20, 1.f, FColor::Yellow, FString::Printf(TEXT("Button Pressed")));

Now, inside of the Widget Blueprint I created a button and a new variable of type of type CameraPawn object.

I then assigned the button’s onClick function to call the CameraPawn’s Test123 function, but when I click the button, nothing happens. Is there a way to solve this?

You created the “Camera Pawn” variable in the widget blueprint? That’s fine, but do you actually set it to something? (CameraPawn = something) Otherwise it’s just going to be null (empty), and you can’t do anything with… well, nothing. If the CameraPawn is your player class, than you can use the GetPlayerCharacter node, cast it to CameraPawn class, and then call the function.

On a side note:
You usually want to set up your widgets in a way that they don’t really have to read data. I usually only respond to events in widgets. (e.g. OnPlayerKilled which is a dynamic multicast delegate in my PlayerController class). In the widget blueprint, I bind an event to that delegate during OnInitialized. So this way all the data is getting fed for the widget. You can also declare functions in C++, make them UFUNCTION(BlueprintImplementableEvent). This way you can call it from C++, but the functionality can be implemented in blueprints.


Yes! It worked, thank you so much.