I know there are a lot of posts with the same question and same answers, but there is never a real clear answer about this topic.
I know there is not a ‘destroy widget’ action and I wonder why not.
I know we use ‘remove from parent’ to do so but the widget object stays there.
Some people ask why we would want to destroy it instead of keeping it in a variable
or something for future use (LOL). There are a lot of reasons.
When you remove a widget and add it again it runs event constructs again. That
is good BUT! variables and general states of the widget persists from previous
usage…variables don’t reset and states of components don’t reset…in short you
don’t have a new fres widget…you just kinda awaked it again and semi initialized it.
Some people say using ‘collect garbage’ after removing it and not having it in a variable
or linked with anything will actually destroy it and clear the memory from it but
how can we trust and be sure this is real and not an act of faith as we can’t check it?
I even tried binding some widget to a ‘you are still there?’ dispatch from gamemode
to see if its there after removing and garbage collecting and the answer is always
YES I STILL HERE!
You can say…of course it does…because the binding with the dispatcher keeps it in
the universe of alive stuff…
Then what is the way to really be sure it is destroyed and why is there not just a simple
destroy widget that really works?!
By default Top level only is enabled and shows the ones that are
added in the viewport only.
Also is important that slight delay after remove from viewport
and before collecting because doing both sequentially wont give garbage collector
the time to catch the just removed widget and will fail.
Now I did some more tests and I can confirm it works and Widgets finally
get destroyed with evidence of it
So you think in the case we want to ACTUALLY destroy a widget we can make some
custom event like this to be used?
You shouldn’t call collect garbage manually. Just remove the widget and clear references to it, It’s enough. If the widget continues to perform some actions, stop them on the Destruct event.