Saving/loading Object Arrays after game quit/restart?

Hi there. I’m working on my project and I’ve made a basic inventory system which is based upon a array of “object” type which contains the item template items. I’ve figured out a way to move inventory items between levels and also save them. However if i exit the game and enter the world, the inventory resets, not being able to load the items from the array. Here are some screenshots:

Items in backpack, level 1:

Items loaded to backpack, level 2:

Now if I was to change levels, the inventory is saving/moving successfully, however if I were to exit the game completely, here is what I get:

Items not loading from object array but other variables loading successfully:

And here are some of my nodes behind the scenes, excuse my lavish coding, I’m new to UE!

How adding items to the inventory work: (ItemObject is UObject & InventoryItems variable is Object array)

How loading items work (inventory widget):

Saving:

Loading:

Basically save gets called at the end of the level, and I call load on BeginPlay of character BP, as pictured below:

And finally the code I’m using for setting backpack slots:

I’ve been trying to fix this for a while now. I hope someone can help me out. I would really appreciate any tips… Couldn’t find any result I want online, maybe the community can help! :sweat_smile:

Thanks, regards!

It looks like you’re using the game instance. Which will work fine for changing levels, but not when quitting and restarting, as you say.

For that to work, you need the save game:

Appreciate the tip! I’m taking a look at this right now but what I dont understand is some variables are being saved even though I’m using the Game Instance. Currency(gold) does not reset after quitting the game :confused:

EDIT: I’m also using savegame btw!

The game instance gets destroyed when the game stops. Are you sure of the facts?

Hmm, I think I understand now… I’m handling the loading/saving from the Game Instance. However I cannot use the gamemode because it changes between levels. But still, the currency is getting saved even if I quit/restart the game, that’s what is confusing. The two in-game screenshots demonstrate that (dont mind the potion count - its not being saved, however on right corner of backpack you can see the gold currency).

What’s wrong here?! :sweat_smile: This project sometimes makes me feel like I am trying to run before walking…

To be clear, we are talking about stopping the game totally and the starting again. Without the save game there is no way anything can be different.

You would know if you’ve coded something using the save game. If you haven’t it’s basically impossible to have anything carry over between games.

I’m afraid it must a coding error that looks like things are being saved.

I am using save games, the project is kinda big, it successfully saves a lot of things such as “Created character name”, “Created character class”, bools for equipment etc., it’s just the inventory that is not being saved after quitting. The gameinstance I’m using is empty, i just call the saving/loading to/from savegame functions from the gameinstance. Here is a video in depth:

So I believe the issue is not in the saving but somewhere else while saving the items of the inventory (object array)… However as I said I’m new to this and the inventory system is kinda tricky, not sure what’s happening with it when the game is closed.

Ah…

Ok, so why bother with the game instance? Just keep the inventory in the save game. It works exactly the same, except it persists between plays.

I think the easiest way to do it is

  1. On begin play ( first level ), get a reference to the save game and put it in the game instance

  2. Set up a timer to write the save game back to the slot every 5 seconds

  3. Everywhere else, whenever you want to write to the save game, you can just put the variable in the reference in the game instance, and you know it will be written back within 5 seconds.

1 Like

Hmm, I’ll try doing these now. Appreciate the tips! :grin:

1 Like

Yes, if the reference is already a cast to your save game, you can just pop a variable in there when you want, and it gets saved. You don’t need lots of networks to save the whole thing ( it looks like you have ).

Bump, I think this is got to do with the object references I’m using. I’ve done a ton of debugging and it looks like something happens to the variable handling the inventory items, (which is an object array) Do they get invalidated upon exiting the game? Because I can move them through levels with the savegame and all other variables are saved upon exiting except the object array(s). Can this have something to do with garbage collection?

Plis help :slight_smile:

Sorry, was asleep :slight_smile:

Ah… you’re putting object references in the save game. That’s not gonna work I’m afraid, because they are different for each run of the game.

What kind of stuff are you trying to save? ( static mesh, blueprint, … )

It’s a blueprint of Item base which is the parent of all items that includes details of the item object.

Do you mean ‘object’, like this?

If that’s the case, you can’t put it in the save game. You need to convert the object to a struct to save it, and you might as well have used structs the whole time.

You could write a bit of code to convert the object to a struct at the last moment before saving ( rather than re-writing eveything ).

1 Like

Hey, yeah I’m trying to save an object. However if I was to save an object, how could I change my loading items to work with a struct? I appreciate ANY help!

You can’t save an object, that’s the problem you’re having, you can only save and load structs.

Just as you’re about to write to the save game, you have to convert to struct ( and back again for reading ).

The struct will be exactly the same as your object. So, if my object was

image

then your struct would be

Then in the save game

You don’t have to use structs. You can just put the variables in the save game. Then you just have an array of bool, array of int, and larger array of vectors etc.

1 Like

Hey man thanks a lot for this answer. I think I understand now. Last issue for me is to now load items based on the values of the variables in the struct. Really appreciate this!

1 Like

FIXED! So happy =)

Thanks @ClockworkOcean for the help, you guided me in the right direction, appreciate trying to help!

1 Like