Removing Widget Instances

Hi,
So I am creating an inventory system from scratch and I’m working with UMG’s at the moment. When the player presses ‘I’, the inventory shows up, and when they press ‘I’ again, it goes away. However, when I check the debug object, it shows many instances of the widget. Is there a way to only have one instance created at a time and when I remove it from parent, I destroy it as well?

Hey @Zapking9224!

Glad to see someone using the debug tools to track down ways to optimize their game! The issue you are facing is most likely an issue stemming from a bad development choice. What I mean is that you probably want to look at other ways to showing and removing widgets.

Games with an inventory system usually want the player to access the inventory smoothly and easily. Creating it and showing it every time the player presses the “I” key is probably not the best method. Instead, what you should probably do is to create one version of it when the game loads in and then simply show / hide it.

When you run the Create Widget node it will keep creating a new widget over and over again. You can use the Remove Parent node to remove the widgets.

I hope this helped!

Hey @EliasWick
Thank you for the response! That definitely makes sense and I was hoping there’d be a way to create a widget and then have it destroyed. So any time I create a widget, it’s going to take up memory for the remainder of the game time? I’m sure it’s not too heavy but definitely something to keep in mind when optimizing maybe?

No. If you dereference a widget completely, the garbage collection will free up memory. It may not always be instantaneous, it may take 30 or so seconds for it to happen. If it does not, the widgets are referenced somewhere. Assuming you’re not using version as ancient as 4.17…

Regarding something like an inventory or any reusable widget, hiding/showing is better in most cases, indeed. Adding / removing from parent may also be an option in case you want to re-run the widget’s Construct Event.

1 Like