This seems like a slightly silly thing to be confused about, but are UMG interfaces like complex in-game menus intended to be destroyed when not in use (to free up the memory), or simply hidden from view but kept in active memory? The former seems more efficient, but I don’t know what the tradeoff would be on manually creating a widget, adding it to the viewport, and initializing it every time the player toggles something open or shut.
Doesn’t remove from parent technically count as a destruction call, though? I know I can get widgets to run their destructor by un-parenting them.
From my understanding, objects that are not referenced are cleaned up by the garbage collector. If your destructor was being called it’s because when you removed it from viewport, there were no valid references to the widget at that point.
So if you’re removing from parent, but calling create widget again at a later time, does that spawn an entirely new widget or does it just show the previous one removed from parent?
That would create a new one, but if you store that new widget in a variable, it replaces the old one (which is now not referenced). If you’ve got a variable storing your widget, and you add that variable to the viewport, it should be the same widget. At least if I’m correct above. You can always check if the variable is valid, then create a new widget if it isn’t.
But if you aren’t saving widgets to variables to begin with, they aren’t referenced and should be picked up by garbage collection; correct me if I’m wrong on this. I definitely don’t want to be spawning tons of widgets and have them sitting in memory.
Right. If you aren’t storing any references, and you remove from parent, the widget should be collected. If you just don’t want them in memory, that’s fine. If you want to avoid creating lots of the widgets, you can store a reference and remove/add that widget to viewport. Sorry for not being clear.
Nah, I understood you. I just wanted to be certain. Don’t want a memory leak, especially on a mobile platform where every bit matters. I completely understand the keeping reference thing. If I felt my widgets would be in/out frequently I’d definitely store a reference. I do that with a few things now to optimize performance. Might use a small amount of RAM but it’s better than eating up the CPU which results in slower work and more battery drain.
If you’re going to reuse it (e.g. it’s a weapon picker like in ratchet and clank), hide and unhide it.
If it is just needed for a random event e.g. pausing the game (which doesnt happen very often), destroy it and reinitialize when needed.