[Tool] WidgetBox - Recycle your widgets the smart way

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.
WidgetBox_Remove.PNG

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

Download
UE4.16
UE4.17
UE4.18
UE4.19
UE4.20
UE4.21 (Updated 28.12.2018)
UE4.22
UE4.23
UE4.24
UE4.25

Feature Request & Bugs
Let me know.

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.

I don’t use it because I started using Noesis, but generally pooling is better than delete/new so i’d say it’s still worth it.

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 would like an update of it if possible yes. This could kinda serve as an object pool. Because I’m mass-spawning and destroying widgets.

Okay, will see that I do this today.

Version for UE4.19 is up. Enjoy.

Thank you!

Version for UE4.20 is online.

Version for UE4.21 is up 'n running.

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.


[2018.12.08-15.20.20:133][366]LogWindows: Error: === Critical error: ===
[2018.12.08-15.20.20:133][366]LogWindows: Error:
[2018.12.08-15.20.20:133][366]LogWindows: Error: Assertion failed: !IsUnreachable() [File:D:\Build\++UE4\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp] [Line: 1319]
[2018.12.08-15.20.20:133][366]LogWindows: Error: RadarBlip_C /Engine/Transient.GameEngine_0:BP_MyGameInstance_C_0.RadarBlip_C_2 Function: '/Script/UMG.UserWidget:PreConstruct'

Any idea?

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?

I have no problems with unknown times, and this crash doesn’t seem to be occuring every time. Seems to be happening at random points.

which engine version?

I’m using 4.21. I skipped 4.20 and went from 4.19 to 4.21. My own code did not change.

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.

Edit: Got the crash in standalone game.

So you did not have that issue in UE4.19 either?

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.

Cool, thanks! And no I did not have this crash in 4.19. Been on that version for months without a single crash of that kind.

The crash seems to still be happening.

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.