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:
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!
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
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?! This project sometimes makes me feel like I am trying to run before walking…
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.
On begin play ( first level ), get a reference to the save game and put it in the game instance
Set up a timer to write the save game back to the slot every 5 seconds
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.
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?
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 ).
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 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.
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!