1

Currently trying to implement a saving and loading system in a unity project to be able to save and reload states of the game. The project is similar to a tabletop card game where there are cards and stacks of cards. The stacks of cards contain a LinkedList of KeyValuePairs with each node containing a card and its orientation in the pile (true = up, false = down). This looks like this:

LinkedList<KeyValuePairs<Cards,bool>> cardsList

We are already correctly saving the position, rotation and type of the objects in the project using serialization surrogates, the list of cards in them is not saved however. How would we go about creating a serialization surrogate for a LinkedList of this sort as its needed for saving and loading? We have tried implementing this in different ways but have had no luck so far, any help would be appreciated.

Edit: Sorry forgot to mention that we are using a binary formatter for this and not XML

Stroudy
  • 11
  • 2
  • Can't you just turn it into a `List>` and serialise that? Then deserialise back into a list and create a linked-list from that. – Matthew Watson Mar 12 '20 at 16:22
  • Maybe answered here: https://stackoverflow.com/questions/2271582/how-to-xml-serialize-a-linkedlist – Oguz Ozgul Mar 12 '20 at 17:26
  • @Stroudy the same applies to the `BinaryFormatter` .. as Matthew Watson already mentioned: What hinders you to convert it to a `List>` and serialize and deserialize this instead, then later convert it back to a `LinkedList`? `We have tried implementing this in different ways but have had no luck so far` could you show these and where you had trouble with them? – derHugo Mar 13 '20 at 10:42
  • @derHugo and the others, thanks for the replies, had a brief look at converting between linked list to list and vice versa. However we couldn't really figure out how to do this properly. The reply marked as the answer is the solution we thought of meanwhile which seems to be working alright so far. Hopefully its not just a quick patch, but continues working for us. – Stroudy Mar 13 '20 at 16:28

1 Answers1

0

Thanks for the replies, we've managed to fix the issue by creating a custom linked list serialization surrogate that iterates through the linked list and saves each node into the file using a unique reference based on position in the list. When loading data from the file we can then use the same unique reference to retrieve the correct nodes in the right order. Below is snippets of code to show how we've done this (we realise it is probably not the most efficient way but it works for our needs):

Saving data to file:

   `int num = 0;
    info.AddValue("count", cardsList.Count);
    foreach (KeyValuePair<Cards,bool> el in cardsList){
        info.AddValue(num + "id", el.Key.ID);
        info.AddValue(num + "faceUp", el.Value);
        num++;
    }`

Loading data from file:

   `int count = (int)info.GetValue("count", typeof(int));
    string id = "";
    bool faceUp;
    for (int i = 0; i < count; i++)
    {
        id = (string)info.GetValue(i + "id", typeof(string));
        faceUp = (bool)info.GetValue(i + "faceUp", typeof(bool));
        linkedList.AddLast(new KeyValuePair<Cards,bool>(gameController.FindCard(id), faceUp));
    }`
Stroudy
  • 11
  • 2