Unreal Engine allows you to have a variable of type Widget (User Widget / CustomWidget) that is Soft Object Reference. But how do you handle them? Here is a little example:
I made a widget named “CharacterSkinsW”, and another widget named “PersonalCharacterW”.
Inside “PersonalCharacterW” I made a variable named “CharacterSkins” of type “CharacterSkinsW” that is Soft Object Reference.
Inside “PersonalCharacterW” I also have a button named “ShowSkins”, and only if/when the button is pressed the variable “CharacterSkins” should get loaded into memory or else if the players doesn’t access the skins in that session of the game, it is a waste of memory to have the skins loaded if he/she won’t change it.
Now the question is, how do you handle that widget soft reference?
If for example I had a variable of type SkeletalMesh that is soft reference, for it’s default value I can give it a skeletal mesh that I have, and when Async Load Asset is called it knows what mesh to use.
But when it comes to User Widget, if you use that as the type for the soft reference, you can’t give it a default value. But you can also do it like in the above example, and not use User Widget as type and instead use the widget type you want like “CharacterSkinsW”, but how do you handle it?
Can you even use soft reference for widgets as well (to load, create, show)?
In my main project I have a widget for the main menu, and inside it I have a few buttons for starting a session, accessing the available skins, the option menu… And for each button event on clicked it creates a new widget for each of the function.
Now the problem is that when main menu is created, because I have a hard reference to the skins widget, it also loads all the skeletal meshes into memory even if the user doesn’t access that menu, which creates a big memory usage that is not needed all the time.
That is why I was thinking to make all the widgets soft reference, and only be created IF the user uses them.
After further testing on this I came to the conclusion that technically the Parent Class for the widget is UMG.UserWidget but the widget is of Widget Blueprint type, and that is what I should use for the Soft Object Reference type of the variable, and not UserWidget or in that example “CharacterSkinsW”.
Now, even tho inside “PersonalCharacterW”, the reference to “CharacterSkinsW” is of soft type and that is the only place it is used in, when i call the cmd obj list name="CharacterSkinsW" is is saying that it is already loaded into memory (the blueprint is closed, and I restarted the editor to clear it form memory as well). Because it is somehow already loaded, when I call Is Valid Soft Object Reference it returns always true.
Reference to “CharacterSkinsW”:
I made this changes:
Changed the type to WidgetBlueprint
Added this for OnClicked event (in the main project, true and false will have different code, this is just for testing):
When the button is pressed, it goes on true, and then on the cast it fails. So something still isn’t right just yet. Any help on the matter is appreciated.
//Edit1: It seems that it is loaded in memory because of the code from inside OnClicked event even tho it is never called. I removed it, restarted UE and it wasn’t being loaded. But when I added the code back, it stated to appear in memory again. Isn’t the point of soft reference to not be loaded until Async Load Asset is called? In this situation as soon as “PersonalCharacterW” is loaded into memory, it loads “CharacterSkinsW” as well even tho it should only be loaded when that button is pressed.
You should use the base type for the UserWidget as the variable type for your variable. If you select the type from a derived class, unreal will load it in memory as you might call some functions or try to access specific stuff in that type you selected in the blueprint.
So, I assume you should have a soft ref to UserWidget type and then it should work fine.