• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Thomas-Hawk · Dec 29, 2017 at 04:25 PM · playerprefsserializationinventoryxmlsaving

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:

  1. Player drags item into slot

  2. The slot creates a PlayerPrefs entry, unique to that slot, which remembers the ID of the item

  3. It does the same for the amount of the item.

  4. 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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thomas-Hawk · Dec 29, 2017 at 04:48 PM 0
Share

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?

avatar image mossflower Thomas-Hawk · Dec 29, 2017 at 05:00 PM 0
Share

I'd still go with JSON. If you go with a BinaryFormatter, you won't be able to easily examine/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.

avatar image Larry-Dietz Thomas-Hawk · Dec 29, 2017 at 06:13 PM 0
Share

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

avatar image mossflower Larry-Dietz · Dec 29, 2017 at 08:11 PM 0
Share

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.

Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs v. XML File for Saving Data? 1 Answer

Trying to XML serialize objects, getting error that UnityEngine.Transform doesn't implement Add() 1 Answer

Serialization 2 Answers

Storing list aka "save function" 3 Answers

PlayerPrefs Lmitations 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges