x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

[C++] Creating widgets runtime

My problem occurs when I use the "NewObject" function to create my custom Widget, that is derived from UUserWidget.

Inside my custom class I have an UButton that I initialize in "OnWidgetRebuilt". But that UButton remains nullptr after I create my widget with "NewObject". Probably because "OnWidgetRebuilt" does not get called.

My question is as follows: What virtual function should I override in my "custom widget" class, so that when I create a "custom widget" the overriden function is called. (And I can initialize other Widgets in that function).

This is where my UButton get initialized:

 void UMInventorySlot::OnWidgetRebuilt()
 {
     slotButton = Cast<UButton>(WidgetTree->RootWidget = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("inventorySlot")));
 
     // Delegates
     slotButton->OnPressed.AddDynamic(this, &UMInventorySlot::OnPressedEnterDel);
     slotButton->OnReleased.AddDynamic(this, &UMInventorySlot::OnPressedExitDel);
 
     slotButton->OnHovered.AddDynamic(this, &UMInventorySlot::OnHoverEnterDel);
     slotButton->OnUnhovered.AddDynamic(this, &UMInventorySlot::OnHoverExitDel);
     // ~Delegates
 
     Super::OnWidgetRebuilt();
 }
Product Version: UE 4.21
Tags:
more ▼

asked Feb 02 '19 at 01:36 PM in C++ Programming

avatar image

BALKAN BANDIT
12 1 2 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

If you are not aware yet, UMG is just front end of Slate, UI system designed originaly for editor, but it can be used in games, problem is it was not designed to cooperate with Blueprint (most impotently it's classes are outside of UObject class), thats why UMG was made to wrap Slate functions

UMG widgets (UWidget) are just UObject shells for already exiting Slate widgets, and because of this UWidget can contain whole composition of Slate widgets. Slate widgets are also a lot easier to operate in C++. So insted of making UUserWidget and reuse UMG widgets, you should use UWidget and incorporate Slate widget in it, trust me you will find it a lot easier to work with in C++. In UWidget you override this function

http://api.unrealengine.com/INT/API/Runtime/UMG/Components/UWidget/RebuildWidget/index.html

And you return slate widget definition and that it, your function would look like this:

 TSharedRef < SWidget > RebuildWidget() {
 
        return SNew(SButton)
                   .OnPressed(this, &UMInventorySlot::OnPressedEnterDel)
                   .OnReleased(this, &UMInventorySlot::OnPressedExitDel)
                   .OnHovered(this, &UMInventorySlot::OnHoverEnterDel)
                   .OnUnhovered(this, &UMInventorySlot::OnHoverExitDel)
                   .Content()
                   [
                          //You put here SNew with widget that will be inside Button
                   ];
           
 }


only one more thing you need to do is to override this function:

http://api.unrealengine.com/INT/API/Runtime/UMG/Components/UWidget/SynchronizeProperties/index.html

It is called when widget proprieties are changed and you should set changed properties to widget settings and oyu can get Slate widget from here:

http://api.unrealengine.com/INT/API/Runtime/UMG/Components/UWidget/TakeWidget/index.html

Ofcorse you need to learn about slate, there docs here with examples. Sicne you work with UMG you should be aware of how it's concept work as UMG inherets it's workings:

https://docs.unrealengine.com/en-us/Programming/Slate

Look on SWidget API refrence to see list of all widgets, you should already be familiar with them as they already used in UMG

https://api.unrealengine.com/INT/API/Runtime/SlateCore/Widgets/SWidget/index.html

each widget class have nested FArgument class with list of all arguments oyu can use in SNew and SAssignNew, here example from SButton

http://api.unrealengine.com/INT/API/Runtime/Slate/Widgets/Input/SButton/FArguments/index.html

One of big benifuits of using Slate direcly to create custom widget is fact that Slate widgets are not visible by UMG and in UMG composition of Slate widgets feel like a truly single widget. There also more widgets in Slate (just watch out that they aren't part of editor code) and not all of them been added in to UMG, so there a lot more possibilities.

more ▼

answered Feb 02 '19 at 09:40 PM

avatar image

Shadowriver
35.8k 926 169 1104

avatar image BALKAN BANDIT Feb 03 '19 at 08:10 AM

Thank you. This is a great answer!

avatar image BALKAN BANDIT Feb 06 '19 at 08:26 PM

Also could you please tell me how I can avoid hard-coding the content of the SWidget?

avatar image Shadowriver Feb 07 '19 at 05:51 AM

Forward properties of content to your UMG widget. what kind of content you want to put in it? How it suppose to behave and how dynamic it is?

avatar image BALKAN BANDIT Feb 07 '19 at 10:09 AM

So I want to create an UMG widget that has a number of custom UMG widgets inside it and that number is known run-time

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question