Storing list aka "save function"

So, I have this script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class NewBehaviourScript : MonoBehaviour {

    List<Position> mylist = new List<Position>();
    int xpos = 5;
    int ypos = 3;

	// Use this for initialization
	void Start () {
        for (int i = 0; i < 100; i++)
        {
            int x = Random.Range(0, 6);
            int y = Random.Range(0, 6);
            int b = Random.Range(0, 3);
            mylist.Add( new Position(x, y, b));
        }
        DisplayList();
	}

    void DisplayList()
    {
        foreach (Position pos in mylist)
        {
            if (pos.xpos == xpos && pos.ypos == ypos)
                print(pos.block);
        }
    }
}

And its Position class

using UnityEngine;
using System.Collections;

public class Position {

    public string position;
    public int xpos, ypos, block;

    public Position(int x, int y, int b)
    {
        xpos = x;
        ypos = y;
        block = b;
    }

}

As you can see this is pretty basic class and list generation and i use this on for testing purposes so my question is: how to save this to file and load when i run application next time?
I heard about xml but it wont save lists, others said use Unity serialization but i dont understand that because i cant see it saving to file. Pretty much I’am quite lost.

It will be for world generation and saving so any advice on improving this system. Currently DisplayList checks if x,y is free position.

Thanks.

Hi there,

As pointed out by Hellium, a simple text file with space separated values would be simpler for some thing and much lighter in memory size. That’s what I would go for if your save file structure is not suppose to change after defined.

However, if you plan to have different versions and are willing to sacrifice file size to get it more human readable (even if only for debugging) you need to have a XmlWriter to which you append the values and then give it to a XmlSerializer.

You may have to implement some methods describing how each field goes into the xml file, but it’s all good when it’s done. As for serializing the list, methods WriteElement and WriteValue from xmlwriter are your good friends.

Hope it helps

You can use SerializerHelper to save and load data. Just ignore all the stuff about saving gameObjects, and add a variable that holds your list to the SaveGame class, and assign your list to the SaveGame instance when saving.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class NewBehaviourScript : MonoBehaviour {

    List<string> mylist = new List<string>();

	// Use this for initialization
	void Start () {
        for (int i = 0; i < 10; i++)
        {
            string a = "";
            int x = Random.Range(0, 6);
            int y = Random.Range(0, 6);
            int b = Random.Range(0, 3);
            a = x + "," + y + "," + b;
            mylist.Add(a);
        }
        DisplayList();
	}

    void DisplayList()
    {
        StreamWriter file = new StreamWriter(Application.persistentDataPath + "/info.txt", true);
        print(Application.persistentDataPath + "/info.txt");

        for (int i = 0; i < mylist.Count; i++)
        {
            string a = mylist*;*

file.WriteLine(a);
print(a.Split(‘,’)[0] + “,” + a.Split(‘,’)[1] + “,” + a.Split(‘,’)[2]);
file.Flush();
}
}
}
Ok here is finished code to store data to text file.
StreamWriter needs path + “/filename.txt”;
also after StreamWriterVariableName.WriteLine(); you need StreamWriterVariableName.Flush(); so stream actually saves content to file
Hope it helps someone and big thanks to these guys that helped me.