[4.7] Keep pointer variables in-between levels.
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.
asked Jul 14 '15 at 01:57 AM in C++ Programming
Actually, I just had an epiphany and the idea hit me. I didn't put UPROPERTY() on my inventory manager variable and that was the reason why it was being GC. This solved my issue during transition.
answered Jul 14 '15 at 05:07 AM
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.
Hope this can help you.
answered Jul 14 '15 at 03:49 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here