Adding a C++ function and calling it in a widget blueprint

Hi All,

Can you provide an example or tutorial showing how to add a C++ function to a project and calling it from a widget blueprint?

How can I view the c++ source code of an event in a blueprint? Is it possible? The c++ source code of what all things in ue4 can be viewed and edited?

Please help.

Hi,

you need to make your own C++ class based on UUserWidget, lets call it MyUUserWidget. Then create a Widget Blueprint, open it, switch to Graph Editing Mode and open the Blueprint Props. In the Details section > Globals > Parent Class you have to choose your newly created MyUUserWidget class.

Add any functions or variables in your MyUUserWidget.h file like so:

public:
  // expose this function so that it can be called via a node from BP
  UFUNCTION(BlueprintCallable, Category = MyUUserWidgetClass)
  void handleMyButtonClick();

In your MyUUserWidget.cpp:

void MyUUserWidget::handleMyButtonClick()
{
	// implementation of functionality
}

In your Blueprint, you can now right-click in the Graph Editing Mode and choose your function.

Well, to view the source code of BP events you can always search the docs or in the source code as well.

Let me know if this works for you.

HI Trutty,

Thanks for your help. I am trying to achieve video playback in UE4.6. Upon some research, I got to know that Media Textures can be used for the same. However, I could not find out how to do this using C++ code. My plan is to write a function which does the following steps:

  1. Create the media texture out of a .wmv file.
  2. Add the media texture to a wall/cube.
  3. Start the playback so that the video can be seen on the wall/box.

Please clarify if something is wrong in my thought process. Can you provide some sample code / commands which can be used to achieve this?

Hi,

I am unable to choose thenewly created MyUUserWidget class In the Details section > Globals > Parent Class … Please help…

Go through all steps of this tutorial by WCode. When you follow these steps, you should be able to reparent your Widget Blueprint.