Accessing a user widget from c++

Can you access a custom widget whose parent is UUserWidget from c++? I would like to have access to its variable space. I guess what I need is a header file to the custom widget so that I can access it from c++ code. But I don’t know if UMG creates a header when you make a custom widget.

Caveat: I don’t know. I haven’t done this before.

I think what you’d have to do is create a new widget class which inherits from the base widget class. Then, let your user widget inherit from your custom widget class. That should let you add in the functionality you’re looking for and expose the widget variable space you are looking for.

Widget blueprints like all blueprints are not known compile time so at compile time (in C++) you have no information about fields and functions that are added in the editor. Fear not, because there are ways to work in C++ with blueprint data and functionality.

First off make a C++ widget class that is between UUserWidget and your custom widget blueprint. So make a C++ that inherits from UUserWidget and then make a blueprint widget that inherits from your class. Now to get data from BP to C++ there are a few techniques:

  • Declare variables in C++ but make them blueprint read write, whenever you change the values in blueprint the changes are accessible in C++.
  • Declare functions in C++ that are implemented or overriden in blueprint, for example using UFUNCTION(BlueprintImplementableEvent). These functions are accessible in C++ but their workings are defined in blueprint.
  • You can even make a blueprint only variable accessible by declaring getter and setter functions in C++ and implementing them in blueprint to read/write to the blueprint only variable.

You cannot have a typed pointer in C++ to a blueprint type since those types are not known compile time, but you can make everything available to the C++ super class and make a typed pointer to that super class. I hope that makes sense.

Hmm. What I want to do is create an instance of a custom widget and add it as a child to a vertical box in another BP. I was doing all of it in BP’s, but it would be far easier in C.

Ah I misunderstood your question. Unfortunately I’m not so familiar with spawning custom widgets from C++. Perhaps its possible if there is a widget variant of SpawnActor that takes a TSubclassOf parameter. I’m also not familiar with using standard widgets like Vertical Box in C++. Hope someone else can fill in!

I too am trying to do something similar. Due to widgets created in the UMG designer not being available at compile time, you’re unable to access them in C++ which if (like myself) you prefer to have everything in code.

The only way I can see around it would be finding a way to construct all of the widgets you need or creating extended widgets for everything you need or having blueprint calling multiple C++ functions which seems sloppy.
My personal use case is using an UEditableTextBox’s OnTextCommitted to add the text to a child widget on a UScrollBox.

You can sniff around the widget tree for a user widget at runtime, and find widgets by name using FindWidget. Look at UUserWidget::WidgetTree and the definition of UWidgetTree.


You can spawn custom user widgets from C++ by using CreateWidget.

inside has many examples of “Accessing a user widget from c++” in his ActionRPGGame.
Look at his ActionRPGGame in the Github.