0

Sorry if the question is weird, I just began to use Unity & started to code.

I'm using a XML Serialization system to create a savestate. Here is my savestate content :

public int item1_owned;
public int item1_price;
public int item2_owned;
public int item2_price;

item1_owned is the amount of item1 the player actually have. The item1_price define the price of the item1 (stored here because it rise after each buy, like a Cookie Clicker system)

I tried to put it more simply, to avoid creating a new int itemX each time I create a new item. So I tried to put a dictionary but it seems like it need changes for the XML Serialization and I'm not experimented enough to try this. What I would like is to put my Item in a sort of class with a price, a name... But I don't know how to create an infinite number of class and how to works with it.

Thanks for the help, and please don't be mean, I really tried :(

1 Answers1

0

There is indeed issues with serializing Dictionary in Unity. You can use Object to represent the itemOwned and itemPrice which make it easy to serialize.

[Serializable]
public class PriceSystem
{
    public int itemOwned;
    public int itemPrice;
}

Create a List of it and add as many Objects as you wish:

List<PriceSystem> pSystemList = new List<PriceSystem>();

PriceSystem pSystem = new PriceSystem();
pSystemList.Add(pSystem);

Then serialize the pSystemList to json or xml.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • @maccettura You will see fields used in Unity so much. That's because Unity cannot serialize properties. – Programmer Mar 30 '18 at 18:57
  • Sorry for the Dumb question, but it tells me that pSystemList does not exist in the current context when I just pasted the code (Error in the last line). Do I have to put the second part of your code in another file ? – Jacob Freestore Mar 30 '18 at 19:09
  • Nothing in my answer should cause that error. If not sure, copy it like it is. I shouldn't give you any error. If you can't get it to work, edit your question, add EDIT to your question then copy and paste the code that's causing that error. – Programmer Mar 30 '18 at 19:13
  • Indeed it was my bad. Now, sorry for asking something else, but what do have I to do to add a "PriceSystem" with code ? And how do I edit the price of a specific itemOwned for example ? Feel free to not answer if this is too much – Jacob Freestore Mar 30 '18 at 19:24
  • *"but what do have I to do to add a "PriceSystem" with code ?"* It's there in my answer...`pSystemList.Add(pSystem);`. *"And how do I edit the price of a specific itemOwned for example ?"* Do that before adding it to the List. `PriceSystem pSystem = new PriceSystem();` then `pSystem.itemOwned=4;` or `pSystem.itemPrice=2;` then add it `pSystemList.Add(pSystem)` – Programmer Mar 30 '18 at 19:46
  • Ok thanks. With a bit of rework this seems to do the job for me. My last question would be : how to save it to my savestate folder ? I normally put the code in my savemanager file and the variable in my savestate file. If I need to modify my amount of money, I will put 'savestate.gold -= Amount' for example. So how will I be able to save my List now ? Thanks you very much ! – Jacob Freestore Mar 30 '18 at 20:21
  • You can save it. See `DataSaver` script used to save an object from [here](https://stackoverflow.com/questions/40965645/what-is-the-best-way-to-save-game-state/40966346#40966346). Since you are trying to save List of objects instead of Object, you need to add the `JsonHelper` class from [this](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity/36244111#36244111) post to your project too. – Programmer Mar 30 '18 at 21:43
  • Once you add the `JsonHelper` class, modify the `DataSaver` class and simply replacing `JsonUtility.ToJson` in the `saveData` function with `JsonHelper.ToJson` and also replace `JsonUtility.FromJson` in the `loadData` function with `JsonHelper.FromJson`. This will enable the `DataSaver` class to save List. – Programmer Mar 30 '18 at 21:46