Destorying a slate widget inside a UWidget class(custom Widget component)

Hello everyone,

I am following Rama guide on creating custom Widget component in slate that can be used in UMG

TSharedRef<SWidget> UBaseSpellUMGWidget::RebuildWidget()
	SpellUI = SNew(SBaseSpellSlateWidget)

	return SpellUI.ToSharedRef();

I am creating the slate widget like this in code.

The problem is at a later stage, I need to destroy the Slate widget and create another slate widget at its location and I am not able to do so.

	if (SpellUI.IsValid())
// 		ReleaseSlateResources(true);
//		SpellUI.Reset();
// 		SpellUI = nullptr;
 		// New Spell
 		SAssignNew(SpellUI, SBaseSpellSlateWidget)


I have tried multiple ways to try to delete it but it seems the widget persists no matter what. And if I replace the pointer with another widget, the behavior is getting weird. I am sure no other SharedPtr point to that widget.

So is there anyway to destroy that widget so I can create another widget in that location?

UMG builds the Slate layout, calling RebuildWidget on the entire hierarchy, then Slate owns the actual SWidget returned by your UMG wrapper. If you update the pointer held by UMG, then UMG has a new widget but Slate doesn’t.

You could fix this by manually rebuilding the widget yourself, but an even simpler solution would be to wrap your widget with a container like SBox, and instead have your UMG wrapper change the box’s child widget.

TSharedRef<SWidget> UBaseSpellUMGWidget::RebuildWidget()
	SAssignNew( WidgetBox, SBox )
		SNew( SBaseSpellSlateWidget )
		.UMGWidgetPtr( this );

	return WidgetBox.ToSharedRef();

if (WidgetBox.IsValid())
		SNew( SBaseSpellSlateWidget )
		.UMGWidgetPtr( this )

	SpellUI->SetSpell( _BaseSpell) ;


Pretty sure you’re just not really supposed to do that. The framework calls RebuildWidget at particular times, and then wraps the resulting slate widget and fits it into the UMG hierarchy. If you try to recreate it by yourself outside of that sequence, you’re asking for trouble.

Is there some reason you can’t just modify the configuration of the existing widget instead?

Edit: This was in response to original post. Just do what Camille said. :slight_smile:

Okay this solution worked… No idea why I had not thought about that.

Thank you…