I have created a widget that appears when a keyboard key is pressed and the game pauses.
In there I have button that removes the widget and unpauses the game. with a simple “Remove from Parent :Self” Node, which lays inside a custom event called Unpause
I decided it would make sense to have the same key could also un pause the game, so I did the following:
Get All Widget Of Class:PausedUI-->For Each Loop-->Cast to Paused UI-->Unpause
At first, instead of the “For Each Loop” I had a “Get index 0” and it worked perfectly but only once!
My Question is this: does my PauseUI need something more to get destroyed or is “Remove From Parent” enough?
If it is enough, why doesn’t the “All widget of class” Table empty after it is removed?
the widget stays in memory when you use “Remove From Parent”, there is no way to “destroy” it manually as far as I know.
What you could do, which is maybe a more elegant solution then using get-all-widgets-of-class, is to create a variable of the type PauseUI, when you click on the pause button you first check if the variable is valid (if something is stored in there), if not (first time clicking pause, it is empty) then you create a new widget of type PauseUI, assign it to the variable and add it to the viewport. When you want to remove it from the screen just use “remove from parent”. Next time you want to pause, you check again if the variable is valid, now it is valid as you had previously stored the widget in there and you just add the content of the variable to the viewport.
I usually do this in blueprint so I don’t have any code snippet to share for this functionality.
Thanks for your answer, you have proven my fears to be correct.
I was thinking of doing something like that as well…
Shouldn’t the fact there is no way to clean the widget from memory be considered a bug?
I mean if I had not added the unpause call outside the widget itself, I would never have thought of this issue.
I was thinking in that direction myself at the beginning and I still think that it would make sense to have a way to clean the widget but it’s the way they have designed.
I’m not sure if there is a clean process that destroys unused widgets in the background every 60 seconds or so.