How to make saved componets for UMG through C++?

Used this guide to extend UMG to C++

A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums.

But I can’t figure out how to add any components that show up in the blueprint for the UMG widget. I re-parented it to my UUInventoryWidget . How can I have a UScrollbox that when I open it in blueprint before the game launches its exposed there and I am able to move it around and edit it, yet still have control of it in C++? Its not showing up in in UMG.

I did Components.Add(myScrollBox);

How can I get my C++ component to show up in Root-Canvas-mycomponent. So that i can then fill in the rest in UMG but still have it exposed. For C++.

providing as much info on this as you can would help a lot. Thanks!

Hey there,

I have needed to do similar C++ Link to a widget in my own UserWidget class. I didnt try to add it through code however as I would prefer to have the flexibility of changing the layout at any time.

Instead what I do is match the class and/or name of the item in RebuildWidget(). I call super first to make sure all the widgets are made and then match it and use the widget. Here is an example where I just match the class (I made a special menu UMG and Slate class)

TSharedRef<SWidget> USubmergedOptionsMenuUserWidget::RebuildWidget()
{   
	TSharedRef<SWidget> OurWidget = Super::RebuildWidget();

	for (UWidget* Widget : Components)
	{
		if (Widget->IsA(USubmergedMenuWidget::StaticClass()))
		{
			//found it now do whatever you need to it, store the pointer etc
			break;
		}
	}

	return OurWidget;
}

What this let me do, is then programatically add menu items to our menu widget here and control all input and functionality in c++. But it also lets us put that widget inside any layout controls like vertical boxes etc in UMG and thus non programmers can just move it around.

Of course I am totally reliant on it being added manually to make it work, but I felt that was ok for the flexibility of layout. On a side note, putting it in RebuildWidget means that code also runs in UMG. So be careful, things like the player owner are not set. The bDesignTime flag is your friend here.

Each UWidget class has a function on it to then get the actual slate widget, so you can then do whatever you want!

Let me know if you need any more info

Got your comment via email, but cant see it here atm. But the answers are:

Where/How would you forward declare RebuildWidget? - This is a function on UUserWidget, just override it in your derived class. Make sure you are including
the relevant slate classes in your derived class header. This is what have (but I also do some slate animation handling)

#include "Runtime/UMG/Public/UMG.h"
#include "Slate.h"
#include "WidgetAnimation.h"
#include "UMGSequencePlayer.h"

No need to call it, it happens automatically and happens in both the design view and at runtime once you create it

How do you implement that. Where do you put that rebuild function and call it?

Its not happening automatically and I don’t know why. Construct() also does not run automatically.

Using 4.5

The widget is a umg blueprint that derives from my class UInventoryWidget

Its working now I have a UE_LOG printing through it but, how would I change all the texts names? Widget only has access to tooltipname, when i try to change tooltipname nothing happens.

But it is printing my log in the for loop when i choose a button or anything that’s in my widgettree.

Just trying to figure out how to modify stuff like maybe rename every object to 1 name just to test.

Ok got it, I just cast to the widget type in the for loop and modify specific members. Thanks!

If your UUInventoryWidget is based on UUserWidget, then you do in that class:

eg

UCLASS()
class UUInventoryWidget : public UUserWidget
{
	GENERATED_UCLASS_BODY()

	virtual TSharedRef<SWidget> RebuildWidget() override;
}

You won’t need to call it, Rebuild Widget is the function the UMG system uses to create the slate widgets from their UMG classes. Just make sure you write your code after the Super like in my example earlier.

How are you opening/creating the widget?

What version are you on?

Is it happening in the UMG Editor Preview?

We are creating our via the player controller like so:

< - Header ->
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = UI)
TSubclassOf<UUserWidget> WidgetTemplate;
 
UPROPERTY()
UUserWidget* WidgetInstance;

<-  CPP ->
WidgetInstance = CreateWidget(this, WidgetTemplate);
WidgetInstance->AddToViewport();

Excellent :slight_smile:

The next question depends on what you are trying to edit exactly. What are these text names? What type of widget?

The basic answer is that the UWidget of the type you want, will have functions and a protected pointer to the actual slate widget, which if you get access to (maybe by extending the class or writing your own custom UWidget class), then you can do everything you could in slate.

What I have done is wrote a custom Menu Widget as both a slate and UWidget class. This class has data/functions to let me tell it what all the menu options are and bind delegates to them. So the my custom USerWidget classes find this widget and fill in whatever menu options they want.

Without knowing exactly what widgets you are dealing with and what exactly you are trying to do c++ side, its hard to know exactly where to point you. So let me know some more info and I’ll try and help

Did this change in 4.6? Getting Unidentified in for (UWidget* Widget : Components) at components