Inventory System Save System - One Object Spawns on Play After Being Destroyed

Alright, I’ve moved that code to construct before anything else. That doesn’t seem to have changed the functionality

There’s no current function to have multiple chests, but I plan to add another one. I’m not sure how well it’d work because I haven’t tested but

I’m also having this issue- if I don’t save the game, when I load, none of the items spawn, like so.

sorry didnt see the reply before,

i can only answer in more abstact ways now as i cant follow all the changes.

to save and load chests.

  1. chests exist in the level so you’d save/load them to the level data
  2. you can get them with GetAllActorsWithInterface like everything else
  3. you may need to expand on the interface, have an enum for what youre saving actor/chest and then switch on enum to return the savedata, ie chest save data struct and actor save data struct.

you can do this for any number of objects you want to save

1 Like

No worries!!!

I’ve tried to do the steps listed, and I’ve come up with 2 problems. In my chest inventory blueprint and in my chest inventory widget, there are two different inventory components. For the data inside the inventory, it seems to be stored in the widget’s inventory component. Because the widgets will only be open one at a time and because they’re widgets, I’m unsure how to access the widgets to save the stored data inside. I’m wondering if I should either change it so the chest actor inventory is the one storing the data, or if I should save the data in the widget, or if there’s another better way. I’ll try to test it out myself as well.

The second problem is that the data inside ONE inventory is an array. So how would I go about saving multiple chest inventories? Is there a way to do an array of arrays, or is there a better solution?

when you open/construct the widget send the appropriate inventory in as a variable.

you can do arrays of arrays via a struct, so you’d have an array of chestsaves struct and inside that struct you’d have the inventory data

1 Like

Ok, those issues seem to be cleared up, then. It didn’t seem to change the lack of saving of the chest.

I’m having a really particular issue where if I save a game and load back in, the player inventory displays correctly. If I attempt to move any items to the chest, and then save and exit, when I load back in, the player inventory is gone in all 3 ways it can be opened (chest inventory, with the “I” shortcut, and in the in-game menu), and all items are erased from the world. I have been having a hard time understanding why this is, and it may be related to the lack of saving, although that could be a whole other issue entirely that I haven’t caught yet.

| Save All Data | Load All Data | Chest Inventory | Player Inventory | Display Inventory |

you dont seem to be actually saving or loading the chest at all?

the way to do it is exactly the same as items, save the chest class, transform and add the inventory array. on load destroy them all and spawn then set inventory array.

Because it’s hard to reference, I’ve added the save and load to the Chest Inventory and Chest Object Blueprints. I tried to do as instructed. Now nothing is spawning in, not even the items. I think my thought process through this was quite muddled, but I’ve tried to make it make sense. I’m not sure how to make the UI_ChestInventory read the container index from BP_Chest, I’ve written a note in the UI Chest to clear up what I mean.

| Save All Data | Load All Data | UI Chest Inventory | Find Chest Info |

just treat it all the same way you save items but with a different struct.

So S_Item is (class,transform)
S_Chest is (class,transform, inventory)
S_NPC is (class, transform, NPCDATA)
etc

on save you get all actors of X (X is item, npc, chest) calls its save function (item, chest, npc) etc
on load you spawn class at transform and set optional data (inventroy/NPCDATA) etc

Previously I had not saved the inventory data component to the chest structure, so I added that and made some adjustments. Now, every now and then, the objects in the scene (excluding the chest) will show up fine on the level, but most of the time nothing shows up on load. I’ve tried to follow these instructions again, here are my updated blueprints.

| Save All Data | Load All Data | UI Chest Inventory | Show Chest |Structures | Chest Event Interact (calls ShowChest) |

why does your spawnactors say NONE?

only other reason i can see why they wouldnt load is the save data is empty. make sure you test all this with breakpoints or printsstings

It doesn’t say spawnActor NONE in my UE blueprint, but it probably means it regardless.
I got it: when a new gamesave is created, I forgot to make it so a new save game object was created to save to when the player saves. That’s why the save data would be empty, and probably why it wouldn’t spawn anything, either. Unfortunately, this change hasn’t effected the functionality, though now the savegameobjects are valid. It’s likely due to the spawnactor not working. Using the is empty node, I found that on load, the actor data and container struct are empty within the save data level. I’m not sure why this would be. In the Save All Data, I clear the structs before adding the items currently in the world into the array, but I’m thinking the second step there doesn’t happen, resulting in an empty array. I checked if the BPI Chest and BPI Item arrays had items in it when I got all actors with interface. The container array has contents inside, which means it should theoretically loop and be added to the container struct/actor data array. But it’s not doing so. The actor data array, however, is empty when I Save All Data. So I’m not sure what this means…

| Save All Data | Load All Data | Save and Exit Menu |

you clear the contrainer struct but you never ADD to it.

your item loop is working? basically copy that. the steps are the same its just the data is different

The item loop isn’t empty, but it still doesn’t spawn in. I had it in my head that I had to keep track of the container’s indexs, but I realize that was making things unnecessarily complex.
So I put the inventory system and contents in the container struct, save it, and when it loads, I set both to the chest I spawn, that’ll get the chest data correctly to the chest, right?
Neither are empty anymore, that’s good. And the chest loaded in, but looking like this, and I’m getting the following error;

Blueprint Runtime Error: "Attempted to access ChestInventory via property ChestInventory, but ChestInventory is not valid (pending kill or garbage)". Node:  Set ObjectData Graph:  Load All Data Function:  Load All Data Blueprint:  BP_GameInstance
Blueprint Runtime Error: "Attempted to assign to None". Node:  Set ObjectData Graph:  Load All Data Function:  Load All Data Blueprint:  BP_GameInstance

I don’t know why that would be. The inventory in the container struct it is drawing from is invalid, I checked through print string, but it’s added to the structure in Find Chest Info directly from the chest inventory, and that IS valid.

| Find Chest Info | UI Chest Inventory | Save All Data | Load All Data |

The items show up most times on the ground, but strangely, sometimes they just don’t. I can’t figure out the pattern.

you cant save object references, so ChestInventory is invalid.

thats why we respawn it, we reconstruct objects that need to be saved by their properties, ie class, transform and contents

So now I’m looking at the Find Chest Info, and even when I put an item in the inventory, the Object Data variable inside of the chest reads as empty. I’m not sure why this is because this is where I recall storing the items inside the inventory. I’m wondering if I should also save the content struct, because it’s reading as NOT empty, though when I load in, the items in the chest do not load (the slots do load, but they are all empty.) I would think I’m saving all the appropriate things at this point, but I suppose I’m not sure.
Today when testing, it looks like both the chest and items around the map are showing up appropriately, so that’s good.

| Find Chest Info | Save All Data | Load All Data |

i know you’ve made lots of changes but try cleaning up your code and comment everything.

you chest doesnt need all containers, delete that
you second print string isnt connected to anything

you’re checking objectdata the first time and content the second time (i dont know what either of those are either)

you’re probably not seeing these because there is too much going on, so keep it simple and clean

1 Like

Object Data is an array of F_SaveItemStructs that include the class and transform of objects (which I don’t really know why this would be something I need when looking for data on whats INSIDE the chest. That could very well be the wrong type of struct, and maybe that’s why it isn’t working.)

Content is an array of F_SlotStruct that includes an Item ID and quantity of items in the inventory.

The tutorial also created an ItemStruct, but I don’t think I’m referencing that anywhere in the chest save system. It looks to be the structure that. I think this might be the issue but the confusing part was that the player inventory is functional, and it too uses the SaveItem struct.

So I’ve taken a look at the uses in my blueprints of the SaveItem Struct. In the Inventory Slot, the pre-construct pulls from the ‘content’ structure, using the ID to find the item in the data table (visuals here). So what I’m finding is that Object Data means nothing. Content is the one to focus on. I realized I was forgetting to set the content on load, because I’d been so focused on Object Data. Once I did that, I finally was able to see the contents in the chest inventory. I’ve erased most, if not all, of the Object Data parts of the container part of the save system.

One last problem that I can find. I troubleshooted a bit, and I found why the items and chest would, at “random” times, not spawn in on load. I’m not sure why or how, but it turns out that it only happens whenever the inventory is empty. I’ve attempted to clean up all my blueprints, hopefully that helps. Thank you for your patience and help.

| Save All Data | Load All Data | New Save Game |

you setup looks fine and you’ve got the correct flow, so any errors are likely elsewhere.

in theory the itemstruct is inside your slotstruct?

remember use construct (world) not preconstruct (editor preview) in widgets

Optional: If you use the correct base class on your savedata (ie chest class) you can set the variables to ExposeOnSpawn rather than CastToChest->Set

I had followed the tutorial for this part, and the item struct does not have an array inside for the slot struct, but the slot struct is linked to a data table (by itemID, name class)


And the data table draws from the item struct, as shown below in the inventory slot BP.

But after coming back to this a day or so later, I can’t find the error I was talking about here. So I’m hesitant to say, but I think the problems in this thread are cleared up. Thank you so much for helping me out : D

1 Like