- Home /
Need experienced opinion: Saving a players inventory: XML or PlayerPrefs?
So in my hand-rolled inventory system, slots can have items, both GUI elements, dragged into them, and the slot can use PlayerPrefs to "remember" what and how many of an item occupied it.
I.E:
Player drags item into slot
The slot creates a PlayerPrefs entry, unique to that slot, which remembers the ID of the item
It does the same for the amount of the item.
If the slot becomes empty, the slot saves itself as being empty.
All the slots do this independently, so when the game is
reloaded, each slot simply accesses
one playerprefs entry to re-occupy itself with / load the item back in.
So, since that slot is already in the scene (unlike items, which are obtained in the game) when the game starts, playerprefs plus some scripting can put the item back in the slot, or "load" our inventory. I.E., we are saving and loading the item based on that logic.
However, I've been trying to move from a "slots on a grid" model to more of an "items in a list" model.
The difference there, is that the slots won't be there in the first place, rather I will be needing to save information about a list of just the items.
So, My first thought/ option: Use XML to record which items and how many of them are in the player inventory, which is displayed in a list of just the items' draggable / interactive UI elements.
But, since I have never messed with XML data serialization, I need opinion from someone who has! Before I go breaking all my code and stuff, you know?
Here is where I imagine myself running into problems: The player might "lose" an item, any number of ways, in the game! Will it be easy to remove the entry for that specific item in the XML file? Or will I have to be rewriting the entire file every time a change occurs in the inventory?
My second option, instead of XML: Continue to use PlayerPrefs, but disable / hide the "slot" until there is an item actually in it....This way, the items are already being saved in their correct slot, and the feeling of it being a scrollable list of just the items they have can be maintained (they don't have to scroll through empty slots.)
I would prefer to use XML, but only if I can get a validation that it is appropriate here, from someone who maybe has done similar....So what do you think, and what experience do you have with this kind of problem? Should I use XML or continue to rely on PlayerPrefs?
Answer by mossflower · Dec 29, 2017 at 04:45 PM
I don't think PlayerPrefs is ideal for storing larger amounts of data like an inventory, but I don't think XML is the route to go either. I think in general it makes more sense to use JSON instead of XML nowadays. JSON is more lightweight, and Unity has built in support for it. You may want to get familiar with JSON by checking out Unity's tutorials: https://unity3d.com/learn/tutorials/topics/scripting/loading-game-data-json?playlist=17117
Thank you! I was actually reading around other questions on the topic while waiting for an answer. Can I ask you? Data serialization with BinaryFormatter seems to be good for streamlining the saving and loading of a list of variables, which is what I have: a list (or array i guess? The one thats defined by "[]") which I really just want to be able to save and load.
Is JSON or BinaryFormatter a better choice?
I'd still go with JSON. If you go with a BinaryFormatter, you won't be able to easily exa$$anonymous$$e/edit your saved inventory in a text editor. BinaryFormatter is even more lightweight than JSON, but I don't think that is a primary concern in your case. Having your save-data be human-readable helps with debugging easily in the future.
While I agree with $$anonymous$$ossflower in that JSON would be human readable, and therefor easier to look at and debug, I personally prefer to use the BinaryFormatter specifically because it is NOT human readable, so players of the game, who are able to locate the save file, are less likely to cheat by editing the file because of the amount of work it takes to successfully edit it without breaking it.
Just my 2 cents, -Larry
You have a good point, but I'm a fan of enabling cheats/mods in single-player games. I$$anonymous$$O, the easier it is for other people to mess with the game, the better. Obviously, it's different for multiplayer games, but in multiplayer games, you should probably keep all save data tied to a user account online. In which case, you'd probably connect through a RESTful API that probably also uses JSON.