Hello everyone i need some help what do i need to change in this code to be able to serialize (save) this file?
I'm trying to create a inventory file that will save crafted items (this one in particular saves crafted components) i have a almost identical code for saving crafted weapons. Searching the net i couldn't find a way to serialize this object because it uses parameters, but i need to add data into that list and save it. Is there a way around it ? if yes how ? THANK YOU !
the error im getting is
InvalidOperationException: ComponentDB.ItemEntry cannot be serialized because it does not have a parameterless constructor.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System;
using System.Linq;
public class ComponentDB : MonoBehaviour
{
public ItemDatabase itemDB;
public string Slot;
public static ComponentDB ins;
void Awake()
{
ins = this;
Slot = "Slot1";
}
public void SaveItems()
{
XmlSerializer serializer = new XmlSerializer(typeof(ItemDatabase));
FileStream stream = new FileStream(Application.dataPath + "/StreamingAssets/Save/" + Slot + "CraftedComp.xml", FileMode.Create);
serializer.Serialize(stream, itemDB);
stream.Close();
}
public void LoadItems()
{
XmlSerializer serializer = new XmlSerializer(typeof(ItemDatabase));
FileStream stream = new FileStream(Application.dataPath + "/StreamingAssets/Save/" + Slot + "CraftedComp.xml", FileMode.Open);
itemDB = serializer.Deserialize(stream) as ItemDatabase;
stream.Close();
}
[Serializable]
public class ItemEntry
{
public string Name;
public string Data;
public int Amount;
public ItemEntry(string iName, string idata, int iAmount)
{
Name = iName;
Data = idata;
Amount = iAmount;
}
}
[Serializable]
public class ItemDatabase
{
public List<ItemEntry> list = new List<ItemEntry>();
}
public void ManageItemsInv(string input_name, string input_list, int input_amount)
{
ins.itemDB.list.Add(new ItemEntry(input_name, input_list, input_amount));
}
}
Your code are almost OK, except: you missed a parameterless constructor for ItemEntry, then you could not serialize it.
Solution is quite simple: create a parameterless constructor in ItemEntry class:
[Serializable]
public class ItemEntry
{
public string Name;
public string Data;
public int Amount;
//parameterless constructor for XmlSerializer
public ItemEntry()
{
}
public ItemEntry(string iName, string idata, int iAmount)
{
Name = iName;
Data = idata;
Amount = iAmount;
}
}
Related
I have tried alot of solution but no one is working in my case, my question is simple but i cant find any answer specially for windows build. I have tried to load json form persistent , streaming and resource all are working good in android but no any solution work for windows build. here is my code please help me.
public GameData gameData;
private void LoadGameData()
{
string path = "ItemsData";
TextAsset targetFile = Resources.Load<TextAsset>(path);
string json = targetFile.text;
gameData = ResourceHelper.DecodeObject<GameData>(json);
// gameData = JsonUtility.FromJson<GameData>(json);
print(gameData.Items.Count);
}
here is my data class
[Serializable]
public class GameData
{
[SerializeField]
public List<Item> Items;
public GameData()
{
Items = new List<Item>();
}
}
public class Item
{
public string Id;
public string[] TexturesArray;
public bool Active;
public Item()
{
}
public Item(string _id, string[] _textureArray , bool _active = true)
{
Id = _id;
TexturesArray = _textureArray;
Active = _active;
}
}
In order to be (de)serialized Item should be [Serializable]
using System;
...
[Serializable]
public class Item
{
...
}
Then you could simply use Unity's built-in JsonUtility.FromJson:
public GameData gameData;
private void LoadGameData()
{
string path = "ItemsData";
TextAsset targetFile = Resources.Load<TextAsset>(path);
string json = targetFile.text;
gameData = JsonUtility.FromJson<GameData>(json);
print(gameData.Items.Count);
}
For loading something from e.g. persistentDataPath I always use something like
var path = Path.Combine(Application.persistentDataPath, "FileName.txt")
using (var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var sr = new StreamReader(fs))
{
var json = sr.ReadToEnd();
...
}
}
For development I actually place my file into StreamingAssets (streamingAssetsPath) while running the code in the Unity Editor.
Then on runtime I read from persistentFilePath. If the file is not there I first copy it from the streamingassetsPath the first time.
Here I wrote more about this approach
I'm nearly done making my mobile game and I have have a DATA script using what is shown in this video. I have a list which holds the values of different challenges that the player can complete. How would I update the game so that I can add more challenges whilst still keeping the old data.
(The challenge data basically contains whether it has been completed and how far off being completed it is)
I have had a look at this guide but I don't quite understand it. I'm new to serialization.
Thank you in advance :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
[System.Serializable]
public class XMLManager : MonoBehaviour {
public static XMLManager dataManagement;
public gameData data;
void Awake()
{
//File.Delete(Application.dataPath + "/StreamingFiles/XML/item_data.xml");
System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
dataManagement = this;
DontDestroyOnLoad(gameObject);
}
public void SaveData()
{
XmlSerializer serializer = new XmlSerializer(typeof(gameData));
System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
FileStream stream = new FileStream(Application.dataPath + "/StreamingFiles/XML/item_data.xml", FileMode.Create);
serializer.Serialize(stream, data);
stream.Close();
}
public void LoadData()
{
System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
if (File.Exists(Application.dataPath + "/StreamingFiles/XML/item_data.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(gameData));
FileStream stream = new FileStream(Application.dataPath + "/StreamingFiles/XML/item_data.xml", FileMode.Open);
data = serializer.Deserialize(stream) as gameData;
stream.Close();
}
else
{
print("SaveData");
SaveData();
}
}
}
[System.Serializable]
public class gameData
{
public List<ChallengeStatus> Challenges;
public int HighScore;
public int CoinsCollected;
public List<bool> Unlocked;
public int GamesPlayed;
public int currentChallenge;
}
[System.Serializable]
public class ChallengeStatus
{
public int id;
public string title;
public int current;
public int max;
public int reward;
public bool completed;
public bool claimed;
}
First you should have a look at Unity XML Serialization and use proper attributes. You don't totally need them (except maybe the [XmlRoot]) but they let you customize your Xml file. If not provided Unity uses the variable names and uses sub-elements instead of attributes. However afaik this works only for primitives (int, float, string, bool, etc) and lists of them not for your own class ChallengeStatus. So at least for the list of your class you have to provide attributes:
[System.Serializable]
[XmlRoot("GameData")]
public class GameData
{
[XmlArray("Challenges")]
[XmlArrayItem("ChallengeStatus)]
public List<ChallengeStatus> Challenges;
//...
}
Now I don't really understand why you need to keep the old XML file when saving a new one but if you want to keep the current file I would add an int FileCounter .. ofcourse not in the same XML file ;) Might be e.g. via PlayerPrefs or a second simple text file only including the number or something similar.
Note it is better/saver to use Path.Combine for concatenate systempaths) - the overload taking an array of strings requires .Net4. Something like
private string FilePath
{
get
{
//TODO first load / read the FileCounter from somewhere
var paths = {
Application.dataPath,
"StreamingFiles",
"XML",
// here you get from somewhere and use that global FileCounter
string.Format("item_data_{0}.xml", FileCounter)};
return Path.Combine(paths);
}
}
Than you can increase that global FileCounter everytime you save the file.
public void SaveData()
{
//TODO somehow increase the global value
// store to PlayerPrefs or write a new file or ...
FileCounter += 1;
System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
// better use the "using" keyword for streams
// use the FilePath field to get the filepath including the filecounter
using(FileStream stream = new FileStream(FilePath, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(gameData))
serializer.Serialize(stream, data);
}
}
And read the file with the current FileCounter without increasing it
public void LoadData()
{
System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
if (File.Exists(FilePath))
{
// better use a "using" block for streams
// use the FilePath field to get the filepath including the filecounter
using(FileStream stream = new FileStream(FilePath, FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(gameData));
data = serializer.Deserialize(stream) as gameData;
}
}
else
{
print("SaveData");
SaveData();
}
}
Hint 1:
As soon as you provide a constructor for your classes e.g.
public GameData(List<ChallengeStatus> challenges)
{
Challenges = challenges;
}
than you always have to also provide a default constructor (even if it does nothing)
public GameData(){ }
Hint 2:
You should always initialize your lists:
public class GameData
{
public List<ChallengeStatus> Challenges = new List≤ChallangeStatus>();
//...
public List<bool> Unlocked = new List<bool>();
//...
}
Hint 3:
Btw you don't need that [System.Serializable] for XmlManager since it inherits from MonoBehaviour which already is serializable anyway.
I am using a simple method of serializing and deserializing data for my save files which looks like this
//Object that is being stored
[System.Serializable]
public class GameData{
public int units;
public int scanRange;
public int gains;
public int reputation;
public int clicks;
public Dictionary<string,bool> upgradesPurchased;
public Dictionary<string,bool> upgradesOwned;
public Dictionary<string,bool> achievementsEarned;
public GameData(int units_Int,int scan_Range,int gains_Int,int reputation_Int,int clicks_Int,Dictionary<string,bool> upgrades_Purchased,Dictionary<string,bool> upgrades_Owned,Dictionary<string,bool> achievements_Earned){
units = units_Int;
scanRange = scan_Range;
gains = gains_Int;
reputation = reputation_Int;
clicks = clicks_Int;
upgradesPurchased = upgrades_Purchased;
upgradesOwned = upgrades_Owned;
achievementsEarned = achievements_Earned;
}
}
//Method that handles saving the object
public void SaveFile(){
string destination = Application.persistentDataPath + DATA_FILE;
FileStream file;
if (File.Exists (destination)) {
file = File.OpenWrite (destination);
} else {
file = File.Create (destination);
}
GameData data = new GameData (GameManager.Instance.units,GameManager.Instance.scanRange,GameManager.Instance.gains,GameManager.Instance.reputation,GameManager.Instance.clicks,UpgradeManager.Instance.upgradesPurchased,UpgradeManager.Instance.upgradesOwned,AchievementManager.Instance.achievementsEarned);
BinaryFormatter bf = new BinaryFormatter ();
bf.Serialize (file, data);
file.Close ();
NotificationsBar.Instance.ShowNotification ("Game saved success");
}
//Method that loads the object
public void LoadFile(){
string destination = Application.persistentDataPath + DATA_FILE;
FileStream file;
if (File.Exists (destination)) {
file = File.OpenRead (destination);
} else {
UpgradeManager.Instance.FirstLoad ();
return;
}
BinaryFormatter bf = new BinaryFormatter ();
GameData data = (GameData)bf.Deserialize (file);
file.Close ();
GameManager.Instance.units = data.units;
GameManager.Instance.scanRange = data.scanRange;
GameManager.Instance.gains = data.gains;
GameManager.Instance.reputation = data.reputation;
GameManager.Instance.clicks = data.clicks;
UpgradeManager.Instance.upgradesPurchased = data.upgradesPurchased;
UpgradeManager.Instance.upgradesOwned = data.upgradesOwned;
AchievementManager.Instance.achievementsEarned = data.achievementsEarned;
Debug.Log ("Units: " + data.units);
}
Theres a lot of code here but this is so everyone has a clear picture of what the entire system looks like
So the issue with this method is when adding a new value to the dictionary passed to GameData UpgradeManager.Instance.upgradesPurchased I will get an error when searching for data within the dictionary key not present in dictionary
My analysis is that due to the new value being added there is an offset in the dictionary from where the new value is placed and what used to be in that place
What I expected to happen when I first wrote out the code wa the dictionary would just autopopulate the new values and overwrite the old data
For a visual representation of what I mean lets say you have 2 upgrades
Upgrade1,Upgrade2
Now this is saved
Now the code changes and you have 3 upgrades
Upgrade1,Upgrade3,Upgrade2
What I assume would happen is the new value is just added into the save
So I am not exactly sure why this is happening....
Whilst I can't see the exact cause of the issue I would suggest the following:
First, take your save/load logic out of your GameData class and put it into a SaveDataManager class, that way you segregate responsibility.
From there, you can simplify your GameData class down to a struct making serialisation/desrialisation easier.
Then in your main game class whenever you have to load the game you can do something along the lines of:
SaveGameManger sgManager = new SaveGameManager(file);
gameData = sgManager.LoadGame()
This will make your code much easier to maintain and if this doesn't fix your problem it will be a lot easier to find.
Further to this, it will also allow you to build unit tests that verify the integrity of you load and save logic.
I've not had a chance to test it, but your separated and refactored code would look something like this (although it needs some validation checks added and whatnot):
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace temp
{
public class GameLoop
{
private SaveGameManager sgManager;
private GameData data;
private bool isPlaying;
public GameLoop()
{
sgManager = new SaveGameManager("MYSAVELOCATION");
data = sgManager.LoadGame();
isPlaying = true;
}
private void PlayGame()
{
while (isPlaying)
{
//All of your game code
}
}
}
public class SaveGameManager
{
private string saveFile;
private BinaryFormatter formatter;
public SaveGameManager(string file)
{
saveFile = file;
formatter = new BinaryFormatter();
}
public GameData LoadGame()
{
using (StreamReader reader = new StreamReader(saveFile))
{
return (GameData)formatter.Deserialize(reader.BaseStream);
}
}
public void SaveGame(GameData data)
{
using (StreamWriter writer = new StreamWriter(saveFile))
{
formatter.Serialize(writer.BaseStream, data);
}
}
}
[Serializable]
public struct GameData
{
public int units;
public int scanRange;
public int gains;
public int reputation;
public int clicks;
public Dictionary<string, bool> upgradesPurchased;
public Dictionary<string, bool> upgradesOwned;
public Dictionary<string, bool> achievementsEarned;
}
}
And I really would consider switching out your string keys for upgrades in favour of enums... Much less error prone.
So I have an XML file that I am trying to deserialize into a generic list, but no matter what I do, the List stays empty. I went threw the first ~10 Stack overflow questions that popped up about this, but I haven't figured it out yet.
When the program starts up, I call FishContainer.Load(), the file reader reads the file just fine, but nothing goes in the List.
Xml File
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FishContainer>
<Fishies>
<Fish Name = "test">
<WaterType>salt</WaterType>
<Price>1</Price>
<Size>1</Size>
<Aggression>1</Aggression>
</Fish>
<Fish Name = "test2">
<WaterType>fresh</WaterType>
<Price>12</Price>
<Size>12</Size>
<Aggression>12</Aggression>
</Fish>
</Fishies>
</FishContainer>
Fish Container
using UnityEngine;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
public static class FishContainer {
[XmlArray("Fishies"), XmlArrayItem("Fish")]
public static List<FishData> fishs = new List<FishData>();
public static void Load() {
TextAsset _xml = Resources.Load<TextAsset>("fishdata");
XmlSerializer serializer = new XmlSerializer(typeof(List<FishData>), new XmlRootAttribute("FishContainer"));
StringReader reader = new StringReader(_xml.text);
fishs = (List<FishData>)serializer.Deserialize(reader);
reader.Close();
Debug.Log(fishs.Count);
}
public static FishData GetFishAttributeByName(string name) {
foreach(FishData f in fishs) {
if(f.Name.Equals(name))
return f;
}
return null; //throw
}
}
Fish Data
using System.Xml;
using System.Xml.Serialization;
public class FishData {
[XmlAttribute("Name")] public string Name;
[XmlElement("WaterType")] public string WaterType;
[XmlElement("Price")] public int Price;
[XmlElement("Size")] public int Size;
[XmlElement("Aggression")] public int Aggression;
public override string ToString() {
return Name + " " + WaterType + " " + Price + " " + Size + " " + Aggression;
}
}
The problem is you're deserialising the type List<FishData>, but that's not what your XML file contains at the top level, it's a FishContainer, which in turn has a property of List<FishData> inside it.
Also, you can't deserialise to a static property or class, so you need to remove the static modifiers in FishContainer for the class and property at least. Here is how I would correct FishContainer:
public class FishContainer
{
[XmlArray("Fishies"), XmlArrayItem("Fish")]
public List<FishData> Fishes { get; set; }
public static FishContainer Load(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(FishContainer), new XmlRootAttribute("FishContainer"));
using (StringReader reader = new StringReader(xml))
return (FishContainer)serializer.Deserialize(reader);
}
public FishData GetFishAttributeByName(string name)
{
return Fishes
.SingleOrDefault(fish => fish.Name.Equals(name));
}
}
As a rule, personally I try only to use static methods or properties if they don't change or hold the state of something. You can call this as follows;
var fishContainer = FishContainer.Load(_xml);
Console.WriteLine("I have {0} fishes", fishContainer.Fishes.Count);
EDIT: however, as you want it to be statically available with only a single instance of FishContainer, you could do something like this:
public class FishContainer
{
private static FishContainer _instance;
[XmlArray("Fishies"), XmlArrayItem("Fish")]
public List<FishData> Fishes { get; set; }
public static void Load(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(FishContainer), new XmlRootAttribute("FishContainer"));
using (StringReader reader = new StringReader(xml))
_instance = (FishContainer)serializer.Deserialize(reader);
}
public static FishData GetFishAttributeByName(string name)
{
if (_instance == null)
throw new InvalidOperationException("FishContainer has not been loaded");
return
_instance
.Fishes
.SingleOrDefault(fish => fish.Name.Equals(name));
}
}
.. and call like this:
FishContainer.Load(_xml);
var someFish = FishContainer.GetFishAttributeByName("test");
In order to generate UBL-order documents in XML, I have created 44 classes in C# using Xml.Serialization. The class consist of a root class "OrderType" which contains a lot of properties (classes), which again contains more properties.
In order to test that the classes always will build a XML document that will pass a validation. I want a XML file containing all the possible nodes (at least once) the hierarchy of Classes/Properties can build.
A very reduced code example:
[XmlRootAttribute]
[XmlTypeAttribute]
public class OrderType
{
public DeliveryType Delivery { get; set; }
//+ 50 more properties
public OrderType(){}
}
[XmlTypeAttribute]
public class DeliveryType
{
public QuantityType Quantity { get; set; }
//+ 10 more properties
public DeliveryType (){}
}
I have already tried to initialise some properties in some of the constructors and it works fine, but this method would take a whole week to finish.
So! Is there a smart an quick method to generate a Mock XML document with all properties initialized?
It's ok that the outer nodes just are defined e.g.:
< Code />
Well, sometimes you have to do it on your own:
using System;
using System.IO;
using System.Xml.Serialization;
using System.Reflection;
namespace extanfjTest
{
class Program
{
static void Main(string[] args)
{
OrderType ouo = new OrderType(DateTime.Now);
SetProperty(ouo);
XmlSerializer ser = new XmlSerializer(typeof(OrderType));
FileStream fs = new FileStream("C:/Projects/group.xml", FileMode.Create);
ser.Serialize(fs, ouo);
fs.Close();
}
public static void SetProperty(object _object)
{
if (_object == null)
{ return; }
foreach (PropertyInfo prop in _object.GetType().GetProperties())
{
if ("SemlerServices.OIOUBL.dll" != prop.PropertyType.Module.Name)
{ continue; }
if (prop.PropertyType.IsArray)
{
var instance = Activator.CreateInstance(prop.PropertyType.GetElementType());
Array _array = Array.CreateInstance(prop.PropertyType.GetElementType(), 1);
_array.SetValue(instance, 0);
prop.SetValue(_object, _array, null);
SetProperty(instance);
}
else
{
var instance = Activator.CreateInstance(prop.PropertyType);
prop.SetValue(_object, instance, null);
SetProperty(instance);
}
}
}
}
}