So I’m wanting to keep my inventory class in-between levels set to the same variable. I have a GameInstance class setup and it has been working great for my primitive variables. However, I’m trying to work with keeping my inventory object in memory by setting it in my GameInstance. I’m wanting to pass my inventory as I am loading into a battle scenario and I transition into a battle level, My inventory is a child of UObject also. My issue is that once the level transitions, my variable is now invalid. I want to say it is because it is a pointer and looses its address but I can’t say 100% on that.
Any help on how I can keep my variable set? Also I did try to assign it to a reference variable but it complains about GENERATED_BODY() being private, even when I declare a public at the top of the header. I haven’t really worked with reference custom classes in UE4 so any help would be appreciated.
UObject object pass through levels with host in GameInstance class or GameSingleton class all works well in my situation. But i recommand host in GameInstance class, Singleton has much more problem with object references.
Your Inventory classes should be pure data struct or data container and do not reference any Actor references, because actors are hosted in world and will be destroyed on level end, but uobject in gameinstance will not destory. You can rebuild your inventory widget from UObject data in new level begin play event.
This pattern called MVC, model, view, and controller. pure data objects are models, umg widgets are views. gameinstance, gamemode, playercontroller,levelbp are controllers.
Thanks for your response but I wasn’t actually trying to manipulate it via UMG. I was setting a variable and making sure it was being set and kept its data during level transition. Turns out it was being garbage collected. I added UPROPERTY to it and it fixed my issue. I forgot that the engine does that on variables.