- Home /
[SOLVED]How do i make this dictionary survive recompiles?
So i'm trying to make this dictionary survive recompiles, but i don't know how.
public class HexGraph : MonoBehaviour {
public Dictionary<HexCell, List<Edge>> edges = new
Dictionary<HexCell, List<Edge>>();
}
Here is the Edge struct, if that makes a difference.
public struct Edge : IComparable<Edge> {
public int CompareTo (Edge other) {
return cost.CompareTo(other.cost);
}
public HexCell connection;
public float cost;
public Edge (HexCell connection, float cost) {
this.connection = connection;
this.cost = cost;
}
}
EDIT
So i tried what RobAnthem suggested, but couldn't make it work, so instead i updated my dictionary OnEnable()
I'm still marking his answer as correct, because i'm sure it was my fault it didn't work.
Are you saying you want to save the data that's in the dic, the questions is very confusing?
You need to serialize the Dictionary. http://stackoverflow.com/questions/12554186/how-to-serialize-deserialize-to-dictionaryint-string-from-custom-xml-not-us
Sorry for my bad phrasing. I just want the dictionary to not loose it's data when i recompile my scripts, which i believe you do by serializing it.
I have no idea what XElement is, or X$$anonymous$$L in general, so i understand little of the link you so generously provided. I just want to know if there is an easy way of serializing a dictionary.
Answer by RobAnthem · Dec 29, 2016 at 10:58 PM
Basic binary serialization
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("edges.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, edges);
stream.Close();
Basic binary deserialization
FileStream fs = new FileStream("edges.bin", FileMode.Open);
IFormatter formatter = new BinaryFormatter();
edges = (Dictionary<HexCell, List<Edge>>)formatter.Deserialize(fs);
fs.Close();
Your answer
Follow this Question
Related Questions
Class or struct? for multiple turrets...? 0 Answers
Variable updating itself or not (ex: Transform vs Vector3) 2 Answers
How -exactly- do classes and structs work in Unity? 0 Answers
Conversion of a static class to struct? 1 Answer
more Burgzerg Blues 1 Answer