Viewport->RemoveViewportWidgetContent Not working

Hello everyone…

I create my widget something like this



SAssignNew(MainInventoryUI, SInventoryWidget)
			.InvStorage(StoragePtr);
		Viewport->AddViewportWidgetContent(
			SNew(SWeakWidget).PossiblyNullContent(MainInventoryUI.ToSharedRef())
			);

And remove it like this


Viewport->RemoveViewportWidgetContent(MainInventoryUI.ToSharedRef());

However, this doesn’t work.

However if I try to remove it like


Viewport->RemoveAllViewportWidgets();

, this works.

Any reason?

You’re not removing the same thing you’re adding.



**SNew(SWeakWidget)**.PossiblyNullContent(MainInventoryUI.ToSharedRef())


This weak widget is what RemoveViewportWidgetContent is looking for. Try saving it somewhere with SAssignNew and remove that instead.

Okay thanks… But whats the difference between this pointer and that widget pointer??



SAssignNew(MainInventoryUI, SInventoryWidget)
			.InvStorage(StoragePtr);

Can just 1 pointer be used for the both of them?

SWeakWidget is basically a wrapper that provides some helpers to see if your widget is in fact SNullWidget. As far as I can tell, the samples use it when adding viewport content so that you can just drop your last widget reference in order to “remove” it from the viewport. I haven’t carried the abstraction over from the samples, so I’m not 100% sure that’s how it works. You can probably test it out by zeroing out your last reference to MainInventoryUI to see if that removes it from the viewport.

But if you’re in a similar situation as mine and you do not actually want to destroy the widget when hiding it, then you can probably just remove the weak widget wrapper and add just the one pointer directly, yes.