Hi guys,
I’m doing an inventory system, similar to DayZ / Tarkov / Scum.
Spatial slot based widgets.
I’m stuck with some aspects of it, not knowing how to best handle it.
Being a multiplayer I’m trying my best to keep it lightweight, and make use of Widget Reflector, to make sure i don’t inadvertently create multiple ‘ghost’ widgets, nor to create/destroy and re-create them at every hud toggle, because there are many instructions to spawn them, their values, also give shape, location, color code etc. For example, Health is based on all real life nutritional values and body mass, so i want to avoid recalculating certain widgets, as it references big data tables.
Some widgets are ok to be unloaded from memory, but some will have to remain in background, as hidden.
I tried multiple ideas, but I need some expert’s opinions and steering.
First, let me present the idea.
I have 2 main HUDs to act as HUD_Gameplay and HUD_Inventory.
HUD_Gameplay :
HUD_Inventory
As you can see, from one to the other, there are some widgets that are getting duplicated.
Horizontally on top, we have Team Health twice, in each corner and on bottom, Weapon, Quickbar and Player Health.
I don’t want to Remove and Add Child again in each HUD, nor make it hidden and have it in both HUDs at the same time. That’s because both HUDs are never unloaded from memory, therefore if the widgets get duplicated, at runtime i’m running twice with them.
I want to use the same instance of those widgets in both HUDs.
How do i do that without casting between HUDs ?
I tried having just one HUD and have only the middle overlay swap from gameplay to inventory, but the code starts to get spaghetti, once you consider all the other sub-inventory tabs (crafts, health etc). I used a switch on enum, from no hud, to gameplay, to all the others and it works, but it becomes a mess.
I also tried to have 3 HUDs, one being HUD_Persistent which contains only the persistent widgets, regardless of the HUD i have open it would remain visible, and play with the z order, however only one HUD will accept my clicks, even if i put everything on non-hit, the background blur of the inventory will eat my clicks and i can’t reach the quickbar. Inversely, the overlay of the Persistent will eat my click and wouldn’t reach my inventory slots no more. Even if i put them on the same z-order.
For example z-order:
Gameplay - 0
Persistent - 1 or 2
Inventory - 1 or 2
In all cases all are non hit-test (self only), only children are hit, but still no variant works, something still eats my clicks and only one HUD can receive the click, not the other.
So I was thinking another way would be to create and store the reference of the persistent widgets in Player Controller at BeginPlay, then simply reparent them to each given HUD via a function, each time i switch HUDs. Not sure why but it doesn’t feel right to manage the HUDs via PC, though.
Does anyone have a better idea than mines ?
How do i make sure i make use only of one instance of widgets across multiple HUDs without having to recreate them, or end up in spaghetti code to keep them all centralized in one HUD ?
PlayerController is the only option i got at this point ?
If i got 2 huds filling the entire screen at the same time, only one is actually hit-testable or am i doing something wrong ?
Also, how do i destroy a widget from memory ?
Simply clear the reference to the widget and wait for garbage collection ?
Isn’t there another way more prompt ?
Salutes to everyone