Keep Inventory when loading level

Hi !
I followed the Unreal Engine tutorial series to create the Inventory in my game : UMG UI Inventory | v4.8 | Unreal Engine - YouTube

It works perfectly fine excepted when I change the level with an “Open Level”. , My Inventory get reset.

I’ve tried several variations, creating my Inventory in my FirstPersonCharacter BP, in a Custom Game Controller or in a Custom Game Instance, casting if needed. The whole inventory system still works (picking up items and so on), but my inventory keeps reseting when loading a new level.

I did change Instance and Controller in the project settings and I succeed to save other variables thanks to GameInstance.

Does anyone an idea to my problem ?

In Unreal you have two ways of transition.

A “hard” transition (the game will use all resources to load the level, all old data will be unloaded including all actors, the game state, player state and game instance. Open Level is such a hard transition.

A “seamless” transition. Well seamless transition is usually only used in multiplayer. In singleplayer this is rather level streaming which is also the fix for dynamic (moving) loading screens rather than keeping the last frame from when you executed a hard transition. Moving like this even if you destroy your character and spawn it new the game- and player state will be kept.

If you want to save your inventory between hard level transitions and between the player closing the game and reopening it you will have to save the data.

You do that by using a “SaveGame Object”. It has a quite nice interface for BP and is quite easy to use!

Check out the documentation for it since it explains probably better how to use it than I would :smiley:

Hi !

Thank you very much for your answer, I learned a lot ! I wasn"t aware of these two types of transition.
However, I still got the same problem with my inventory.I haven’t finish with the seamless for the moment, I just tried the hard transition using SaveGame Object.

I guess I’m not getting my array correctly, as on load of my project it says “Failed import arrayproperty” :

  • I created a custom SaveGame BP, having a variable of the same type of my inventory (custom array) called “Sauvegardeur”
  • I created a variable in my levels of my custom SaveGame type called “Svr subclass”

Here are my functions I’m using :

On saving :

On loading, called after the Event begin play in my 2nd level:

Maybe I should loop through my inventory/save_inventory and put elements in the other one by one ?

I’m not quite sure what that struct contains but keep in mind that all actors are passed on by reference.

This means once they are destroyed (either by “Destroy Actor” or by switching the level) they don’t exist anymore and your reference is invalid.

If you want items in the next level you need to store the class of the object and ideally a struct with all the stats of that item. Then in the next level you have to spawn that object and provide it with all it’s stats again.

Sorry for the late reply ! Your answer helped me again to understand how that thing work.
However, shouldn’t we need to store somewhere (where date will not be deleted) any information about what actor should be spawned in the next level ?

The above answers/comments are partially correct - however in order to have a transition state SAVED through game state transitions - you need to utilize the GameInstance classes for game related variables and PlayerState class for players… here’s why:

When a state transition happens within the game (Either through custom states in your GameMode or by level transitions, or by logging in/out) your EVENT BEGIN PLAY runs on all your blueprints… LAMENS TERMS: Your actor is re-instantiated. All your variables that you have stored at runtime now become re-defaulted at this point. . . EXCEPT for in the above classes - which hold their variable values persistent through such changes!

in a Networking/Multiplayer game - GameInstance and PlayerState are created for each player in the world - they hold their own instances of these classes. Make sure to setup the GameInstance in the Maps&Modes section of your Project Settings.