Working with UMG using C++?

I want to do logic behind UMG widgets in C++, but I have no idea what to do after this tutorial:,_How_to_extend_a_UUserWidget::_for_UMG_in_C%2B%2B.

I know the tutorial is for C++ to recognize UMG widgets, but what do I do after that? Say that inside of my widget blueprint, I have a CanvasPanel with a Button I put inside of it, I have already extended it with the C++ class using the above tutorial, now I want to reference the button and add a onClicked event for it. I want to do ALL the logic for the onClicked event using C++, even calling the event. How would I achieve that? I’m just asking for a piece of code to help me with this. (What goes in the header and the cpp?)

I just create an OnClicked event within the UMG blueprint and let it call a C++ function I exposed to blueprints via the UFUNCTION macro. Sometimes I have to pass in a class object into the widget to get access to an instanced object, but that’s pretty easy.

Is there a way to do it just through C++ or do I have to do it your way? And how would one reference the button inside of C++? What if I needed to play an animation (like the button moving). I need some code examples.

I don’t recommend doing that in C++, you can certainly try, all the same functions are available, but you will have to access all the widgets and animation data defined in a downstream blueprint class by making heavy use of reflection, searches for widgets by name (brittle/slow), and casting.

The path I generally recommend for people wanting to mix C++ and UMG is what Slayemin is recommending. Have the complex game logic of the widget defined in C++, but do all the event handling, animation triggering in blueprints, and call the corresponding C++ function from the blueprint event. eg. If you don’t want to write the logic for the Play button in blueprints, make a Play function in C++ that’s exposed as a UFunction, and handle the OnClicked in blueprints and immediately call the Play UFunction.

It enables a much looser coupling to exist between game logic and UI exclusive concerns.

Alright, if that is the best option then I’ll stick with logic in C++ and events with blueprints. Thanks.

i made a tutorial about referencing widgets and tiring to put it in the wiki when am done
check it out it may help you

i would recommend for your your situation (as i understand you have a button on your widget is done in the editor and you want it to do some functionality on event click)

make a function in C++ and make it BlueprintCallable

then set up the on-click event in editor for your button then just connect it to the C++ function this is the way i do it.

make visuals and events in UMG and functions in C++.


I have a question. Currently I’m attempting to create an inventory system. What I’m doing is that when an item is picked up, I call a function which is BlueprintCallable. The function accepts the item name and returns the item name. Here is the function in the blueprint:


Inside of Blueprints, it simply sets the text of the textblock to whatever is returned. The problem is that the entire engine crashes when I run this. In my pickup.cpp code, I’m calling the function like this:

UInventoryHUD extentds UUserwidget and it has the function inside of it.

I know it’s crashing because I’m calling it wrong. How am I suppose to call it?

Set some break points in your C++ code and step through each line, one by one, in the debugger and see which line causes the crash.

It might be something as simple as an array out of bounds error, or trying to dereference a null pointer, etc.

Also, you might be interested in looking at the “Drag and Drop” widget stuff. It sounds like it might be relevant to what you’re trying to do, and simplify your life a ton.


I know the error is occuring because of these lines:

If I remove them, everything runs fine. So I guess it is because of how I’m calling/referencing it. There is a UInventoryHUD class which contains the function like this:

All the function does is return ItemName. Then the blueprint is suppose to pick that up. The engine keeps crashing with this error:

Removing those two lines does not result in the error.

Also, if you mean the widget UI designer by “Drag and Drop”, then I am in fact using that.

Ok I managed to fix it, the two lines were suppose to be:

Now that I know those are running, I want to know how I can have the function in the BP pick it up. This is the function in the BP:


I’m pretty sure it can’t just run by being there. What am I suppose to connect it to, to make sure that it runs when it is called?