Child widgets not being fully removed

I have run in to an issue when attempting to remove children from a panel in UMG (specifically a ScrollBox) where the child widgets do not appear to be fully removed from the game and in some cases interfere with logic meant for child widgets that replace them (the scroll box has it’s child widgets removed and is repopulated periodically). I have replicated this is a blank project and have attached the level blueprint for it and the on screen output below. Initially I was using the ClearChildren node instead of GetAllWidgetsOfClass->RemoveFromParent combination but that had the same results.

After running this logic twice in game I have a total of 10 BPW_TestChild widgets when it should be 5 and printing the display name of the parent of each of those 10 widgets shows that 5 of them have no parent.

I have also tested adding a delay for a short amount of time before printing the number of TestChild widgets in case garbage collection is taking longer than expected and the problem still persists.

Is this a bug with UMG widgets or is there some error I am making in my blueprint logic? Any help would be appreciated.

Thanks,

This is actually working as expected. When you remove a widget from its parent, it doesn’t destroy the widget. I can’t remember why, but they have some reasoning for it. If you run the garbage collector manually, it should pick up on them and destroy them. Typically I just run all code within the widgets themselves, so when they’re removed from parents their code stops working.

Warning: Running the garbage collector will cause a noticeable stutter in your game, it’s a very heft process and it’s highly recommended you don’t use it.

Thank you for responding, now that I know this I have begun work on an alternate implementation that does not rely on destroying widgets.

However it seems very odd to me that there is no way to fully destroy a widget without having to run a very expensive operation. Is it expected that once a widget is created there will never be a situation in which it is no longer needed? Or is there some technical limitation in the engine that prevents widgets from being destroyed in the same way actors are?

I’m honestly not too sure so I won’t comment. Like most of these things - there’s usually a reason. I just kind of work around it. Mind you I can end up with a lot of widgets in memory after a while.

Hello ,

As null7238 stated this issue has already been reported to the developers and this is by design. This appears to be a current limitation. I hope this information helps.

Make it a great day

Unfortunately I am having a similar issue where I need to replace previous child widgets, as it acts as an initiative bar for the enemies in my game. In the same map, you can have multiple encounters. If I do not clear the previous children, there would eventually be more enemies on the screen than you could fit there. But it sometimes keeps old children and will not rewrite them or will not add new ones in their place. What should I do? Maybe just add on to them and make the old ones invisible?

Hi Mewbits,

We ended up refactoring our code to no longer create new buttons but to modify the existing ones which has worked quite well. Making the old widgets invisible is a possible solution as long as you don’t accidentally get a reference to one of them instead of one of the new ones. If you can find a way to reuse the widgets that would also work. Perhaps set unused ones to be hidden, then when you need to use a widget, iterate over them until you find a hidden one then modify it for the new enemy, if you reach the end of the list without finding a hidden one then make a new one.

Hope this helps!