Save/Load with "object" type array not working?

Hi,

I’m trying to implement saving and loading my inventory and equipped items. My entire inventory system is based upon a array of “object” type which contains the ItemTemplate items.

Now I’m trying to save and load this array of “object” type, the save seems to be working, but when I try to load it back in in my controller, the saved array i’m trying to load is empty and I have no clue why.

See screenshots for some of my save/load code.

the strange thing is, however, that i’m doing exactly the same thing with a array of “Texture2D” type and that is working perfectly.

Any clue why it is not working with an array of “object” type?

And if that’s not possible, how would you go about saving/loading inventory items?

Thanks in advance!


My guess is, object instances are invalidated when the game is closed? While textures are gotten straight from your content folder AFA I understand…

Now I tried saving the Item_Struct that’s on my ItemTemplate, and then re-building the item actors with the item_struct info. but again no luck, the array is also empty and using struct also gives me the problem that it can’t be empty, it will always use default values, which makes my inventory say it’s full and can’t pickup any real items that dropped.

I guess I will try populating my itemTemplate with plain variables rather than the item_struct. Then on load, rebuild the itemTemplates based off of the variables. That’s gonna be a lot of work but i guess it’s worth a try, this **** really needs to work :slight_smile:

The short version is that you can not save references. Sure the references are passed to the save game object, but then when you load a new map or restart the game, the references are lost. That’s why, if you check the references, they will be there in the save, but not in the load events.

I too have recently started fiddling with a save/load system for inventory and the like.
As far as I have researched, the consensus is to save the unique properties (probably in specialized structs) of a referenced object, and when loading construct that object and pass in the properties.

I can provide some screen shots of my bp setup if you wish to have an example.

1 Like

I got it working! :slight_smile: :slight_smile:

I used the Item_Struct to save info about my items, then I did “spawn actor from class” to respawn the items and set back its values.

The reason why it first said my inventory was full, and everything got filled with default items was a stupid bug from my side and had nothing to do with struct filling out default values, I just had to do a is valid check when filling my array to see if there was an item already there.

So glad i figured it out, i’ve been stuck with this saving inventory feature for a week now :frowning:

1 Like

Stellarbe,

could you post a Screenshot or a working Version of your saving Setup?

I have the exact same Problem with the inventory. You use, as far i can see, the inventory from the tutorial of Wes Bunn. I try to save to. But i have one Problem.
If i save the Array with object, and the items are in order: Slot1, Slot2, Slot3. All is perfect and loads good. (But i can’t save Array of Texture2d)

But if i move the items, let’s say: Slot1, Slot8, Slot11, then if i save the Array, only two items are saved. So, as soon i move the items and it doesn’t matter how many items
are in the inventory, always are one item missed by saving. For clear: I have 15 Items -> 14 are saved. I have 12 Items -> 11 are saved, and so on.

Could you help me out?

Greetings
Beregron

Hey Beregron,

My code is a bit too complex and confusing atm to post screenshots of.

I’d need some screenshots of your code to help you out. but the forum adding images feature seems to be broken atm.
you can send me some screenshots of your code to rebelcampgames@gmail.com if you want. I’ll be happy to see if I can help you out.

Hey thank you. As soon as i can, i’ll send you some pics.

So this is more confusing then it looks. So most if not all object references work when passed to a save game object and then when game is exited and relaunched load completed. The reference is valid… easy piece set array element to saved array element. But in some cases the object reference isvalid and in array works perfectly well in game but when you exit and relaunch the object reference is in the array but its invalid… The whole point of putting it in the array was to reference it later when not loaded in the screen… no otherwise what do you need a saved array for? AKA it cant be used for functionality which is the whole point of the save and passing the reference. This is nonsensical as far as i can tell and has nothing to do with arrays or the reference itself. It has to do with how the object is getting spawned. So in my case im making skill widgets and when player learns the spells i add that learned spell from widget to list. Add that list to saved file and i want to learn the spells on load. Sounds easy i got the list got the skills they can be learned. It works if you create array well game is running and you use key event to learn spells in the saved array. All valid all functioning skills are learned. If you exit the game load back in. The reference to the skill is invalid but still in the array. Doesnt work obviously cause its not the same object. So the problem is clearly with the widget itself and its construction. AKA its creating a unique instance of that object every time the game is created and for whatever reason its doing it in a nonsensical way. Because its the only widget i have found that does it for me but its clearly a wide issue and from what i can see not a fixed one or even well known. Basically a valid reference which should be valid once again on load is not. There for it will never be valid and can not be fiddled with to make it work cause the code works as is. The reference just invalidates only on load. How do you get a reference to an object that wont hold its value?