I have a best practise type of question. Basically, I was wondering if it would be more effective to remove my invetory widget from the HUD with RemoveFromViewport() when the inventory should not be displayed - obviously uing AddToViewport() when it should (i.e. when input is pressed).
Or should I leave it on the viewport and just use SetVisibility(ESlateVisibility) to set it as hidden when it shouldn’t be displayed and visible when it should? i.e. it stays active on the viewport even when its not displayed.
I am just wondering how this is normally done? i.e. what is the best practise
It depends on your widget really, If your inventory takes a very long time to construct then hiding it will probably makes more sense, otherwise you should just remove it from viewport when it is not used.
Removing from viewport has many advantages, the widget will not be referenced anymore and will be eligible for garbage collection and after being garbage collected it will frees up memory especially if you use a lot of brushes and materials in the widget. Opening and closing the inventory in rapid succession won’t be a problem, because of the way unreal engine manages UObject enables it to use the very same widget object that has been marked as pending kill and revive it to be used as the return value of MakeWidget / NewObject, it is amazingly fast without unnecessary memory allocation, it is what makes unreal engine able to spawn continuous projectile bursts without significant performance hit.
Btw, i have never encountered a widget that needs a long time to set up (construct), but I see how it can happen in games that put significant part of their game logics (for whatever reason) in the UI.
Thanks, that is really helpful and a great explanation. I definitely feel based on what you have said that removing from the viewport is the best approach, so I will stick with that one. Thanks