Download

Preserving object instances when changing levels

Hello everyone,

our problem is as follows: we have a player class which contains an array of objects representing the player’s inventory. We’d like to be able to save this inventory in order to transfer the items over to another level. We’ve tried to achieve this using a SaveGame object, but all saved object instances are lost upon loading a new level. Additionally, we tried implementing archive serialization according to this example: Loading a Saved Game - UE4 AnswerHub, but the same problem remained. Is there a good way to preserve dynamic objects when changing levels/save objects in general?

All actors are deleted when quitting a level (and you’re quitting a level when joining a new one). So you can’t save actor references through out different levels.

There are several things you can do to achieve the same goal though:

  • use level streaming -> an ideal idea in certain situations where it is logical to use it (IE: the player character is just walking from a place to another in your world)
  • use the game instance -> an ideal idea when it comes to saving things from a menu to a level or from the level 1 to the level 2 for instance, however as I said you can’t save items so you’ll have to basically save the current state of the inventory: make a structure composed of an item_class and an integer (the amount of item of that type that you have in the inventory) then save it and restore it when entering a new level
  • use save/game -> depending on your needs you may need to couple this solution with one of the previous one because it is the only way to store information from an application launch to another, you’ll logically have the same kind of structure that I’m talking about above and you’ll store in your save/game

That’s it for the solutions I have in mind :slight_smile:

Conceptually speaking, you don’t really need to save the object instance. You only need to save the object instance properties and then rebuild your new object with those saved properties. Structs are a great way to do this.

The struct solution is the first thing come to mind, and something we desperately wanted to avoid because every item class has a lot of properties. But since both of you recommend it, we’ll give it a try.
Thank you both for the switft answers :slight_smile:

Yeah, structures in blueprint are quite bad and quickly become a mess.

I suggest you build your structure using C++, not sure how to make it a common variable though.

Yeah, structures in blueprint are quite bad and quickly become a mess.

I suggest you build your structure using C++, not sure how to make it a common variable though.

+1, it become a mess when it come to structure in arrays