Widget, add elements at runtime?

Hi, I am trying to add images to a grid in one of my widgets at runtime, but I can’t find any way to do this? I’m only finding nodes for working with variables that you’ve already predetermined.


Basically, I wish to add these pictures to the grid if the condition returns true.

1 Like

Your problem here is that your Icon is a Texture2D but 'Add Child to Grid’s Content input pin takes a Widget.

As part of that true branch, you’ll need to create a Widget configure that with your texture and add that to the grid (instead of adding the texture directly).
The wrinkle is that you can’t (that I’ve been able to find) spawn the built-in widget types (like Image) dynamically. You’ll have to make your own Widget Blueprint that contains an Image Widget and spawn that.

Or, if you’re comfortable with C++, you could write your own C++ function that spawns a UWidget based Widget (as opposed to UUserWidget). That’s what I’ve done, but it’s not for everyone.

1 Like

I could do that, the array and class is c++. I suspect wrapping it in it’s own blueprint is going to complicate things with getting the info from the array anyway.
Do you have a quick example what you’ve done? I’m still wrapping my head around mixing BP and C++.

Edit: Actually wrapping it in a BP was simpler than I thought:

Now I just need to make sure that it adds each item to a different column per iteration and figure out how to make it use the alpha channel when setting the texture. It uses the alpha before setting the brush, but not after for some reason.

Yeah, it doesn’t really add to the complexity all that much which is why it’s a fine option. Note: You don’t need that cast, Create Widget is pretty smart and returns the right type (that’s probably what the note on that node will tell you too, something like “trying to cast to a type that the input already is”).

Sure!

header:
UFUNCTION( BlueprintCallable, Category = "UI Utilities", meta = (WorldContext = "WorldContextObject", DeterminesOutputType = "WidgetType") )
static UWidget* CreateNonUserWidget( TSubclassOf< UWidget > WidgetType, UObject *WorldContextObject );

cpp:
UWidget* CreateNonUserWidget( TSubclassOf< UWidget > WidgetType, UObject *WorldContextObject )
{
	const auto WidgetOuter = Cast< UUserWidget >( WorldContextObject );
	if (!ensureAlways( WidgetOuter != nullptr ))
		return nullptr;

	return WidgetOuter->WidgetTree->ConstructWidget< UWidget >( WidgetType );
}

(The markup here gets it to act like CreateWidget and output the actual type you specify with the class selection so that you don’t need to cast the return value when using the function in blueprint.)

I’m not 100% sure what sort of other limitations there are to the widgets spawned this way… but so far it’s been fine for the things I’ve generally had to do.

1 Like

Hey, so the image is working fine! However I added more elements (EG text) to the “child” widget, but for some reason only the image renders. I feel like I am missing something obvious.



Any ideas?

Not off-hand. I’m not exactly an expert in UMG, though I can get by.
I’ll have to tag out here. Hope someone else is able to pop in and help you out though.

1 Like