Why?
Object creation and garbage collection is a time consuming task. Reserving and freeing memory all over the place also causes memory fragmentation, which is something you want to prevent. WidgetBox is a Object Pool specific for widgets which will keep removed widgets in a pool for reuse.
Solution: WidgetBox!
WidgetBox is the smart way of handling that issue. The WidgetBox will create widgets for you as needed but will save them for reuse when the widgets got removed from parent. It’s useage is very simple (See NoGo section below for what you should NOT do) and keeps the code slick.
NoGo
As I’m not able to set a hook in the engine when the widget is added to a parent, there is a restriction that you must take into account. NEVER EVER ADD a widget from the WidgetBox **TWICE **after getting it from the Box.
Go Green - Widget Recycling
The WidgetBox will only create new widgets if there is no free widget of the specified class. To make a widget available again, remove it from the parent. The widget will go to the Box itself to be marked available again.
Inheritance
To use a widget with WidgetBox, it must inherit from WidgetBoxWidget.
Features
-Very simple setup and usage
-New widget only get created when pool is empty
-Removing a WidgetBoxWidget from parent places it back into the box.
-Multiple Box instances if needed
-One Box, All classes you want (inheriting from WidgetBoxWidget)
-The power of C++
How to set it up?
1: Download and place the contents into '…/MyGame/Plugins
2: Restart Editor
3: Enable Plugin in Edit->Plugins
4: Restart Editor
The main intention of this plugin was to bypass the issue that widgets do not get garbage collected. **With UE4.19 this issue is fixed. **
Does actually anyone made use of this plugin and needs an update of it? If you are only using it because of the now gone garbage collection issue, you should switch back to spawn you widgets as needed.
Im know, but the “NoGo” restriction is to big a bummer. This should be doable as it is engine default behavior. Someone that does not know about that (and it is not obvious at all when grabbig the widget from somewhere), and things go boom.
I sometimes get this crash. I have confirmed it only occurs with widgets that make use of the widgetbox. It says I have stuff erroring before pre-construct.
Hm. Not sure what could cause this. Does this occour everytime you execute a specific section of your code? As the error is script related, do you have issues with unknown types, like problem with a struct after renaming?
trying to reproduce it. Getting a widget from the box each tick and executing a PrintString in PreConstruct works fine. Can you share some Blueprint code? Can also send me a private message.
The problem was GarbageCollection related. I actually failed to mark the pools that contain the returned widgets as UPROPERTY. This issue should have been present in the earlier versions…
I updated the download for UE4.21.
hm, I tested it for over a hour and it did not happen again on my end, collected garbage every frame. Do you have a C++ project? Maybe deleting build and intermediate files might help. I’m not available until end of january.