Hello @bj_to0
I don’t think using interfaces would be much of a improvement, since you are holding a reference in a variable, which will block garbage collection.
You could make a dedicated manager (something like your HUD right now) as a actor component that you could attach to your player/controller, and use that to handle calls for your widgets. You wouldn’t store anything, just remove and create widgets as and when required. They are mainly for visualizing data, they shouldn’t hold anything important or game critical. You can take advantage of interfaces to handle communication and avoid unnecessary hard references.
I believe even when you Remove from Parent but store a reference to said widget in a variable, it still has the same memory cost, so having all of them in memory is just costly. Depending on scenario it might not impact performance in a noticeable way, but I would suggest you take your time when designing this system. In the end it should be more time efficient, since you won’t have to constantly keep going to it, when things will break or you need to add something new.
Also it’s not like you shouldn’t store them in variables, you absolutely can, just be mindful of possible reference chains.
I think you could make a “handler” function that you could feed various information and class of widget to be created. For destruction, perhaps you could implement self destruct logic upon certain events.
It’s really just speculation since I don’t see the big picture (only you do
)but having that many references just sitting in memory, might not be the best choice.
Problem being is said references also have references to other components / actors, and said actors hold more references, things can spiral out of control pretty quickly and it’s a chore to clean it up.
Hope that helps. if you have more questions, I’m happy to help. Have a great day.