Save and Load Problem, Dont quite understand.
Hi, I have a problem.
I cant work out why I cant seem to load and unload a class with three lists in it.
It keeps giving me this cast error:
InvalidCastException: Cannot cast from source type to destination type. EndlessManagerScript.LoadChunk (Vector3 fileNamePos) (at Assets/GAME ASSETS/SCRIPTS/EndlessManagerScript.cs:298) PlayerControlScript.Update () (at Assets/_CUSTOM ASSETS/SCRIPTS/PlayerControlScript.cs:204)
My class is:
[Serializable]
public class blockData
{
public List<string> objectNamesToSerialize = new List<string>();
public List<Int32> xPosToSerialize = new List<Int32>();
public List<Int32> yPosToSerialize = new List<Int32>();
}
My save method is:
try
{
Stream stream = File.Open(Application.dataPath + @"\Saves\Chunks\chunk " + thisChunkFilenameNumber, FileMode.CreateNew);
BinaryFormatter bformatter = new BinaryFormatter();
data.objectNamesToSerialize = objectNamesToSerialize;
data.xPosToSerialize = xPosToSerialize;
data.yPosToSerialize = yPosToSerialize;
bformatter.Serialize(stream, data);
stream.Close();
}
catch (UnauthorizedAccessException) { print("SAVING ERROR!!!"); } //many more exception might happen, check documentation
}
And my loading script is just:
public void LoadChunk(Vector3 fileNamePos)
{
{
Stream stream = new FileStream(Application.dataPath + @"\Saves\Chunks\chunk " + fileNamePos, FileMode.Open,FileAccess.Read);
BinaryFormatter bformatter = new BinaryFormatter();
data = new blockData();
data.objectNamesToSerialize = ((List<string>) bformatter.Deserialize(stream));
// here is where I've tried lots of different ways of reading it, i even mucked around with
// binaryReader and had the same problems. It cant seem to cast back into a new
// instance of the blockdata class for some reason.
stream.Close();
print(data.objectNamesToSerialize.Count);
File.Delete(Application.dataPath + @"\Saves\Chunks\chunk " + fileNamePos);
}
}
Not sure what I'm doing wrong. Thought I'd be able to save a list and then reload the list as is. Could anyone help me please?
Yeah Ive tried stuff like:
data = ((blockdata)bformatter.deserialize(stream);
or
data = bformatter.deserialize(stream) as blockdata;
Same error, cannot cast to destination.
Don't know why.
it says:
emptyRoomGenScript+blockData UnityEngine.$$anonymous$$onoBehaviour:print(Object) Endless$$anonymous$$anagerScript:LoadChunk(Vector3) (at Assets/GA$$anonymous$$E ASSETS/SCRIPTS/Endless$$anonymous$$anagerScript.cs:297) PlayerControlScript:Update() (at Assets/_CUSTO$$anonymous$$ ASSETS/SCRIPTS/PlayerControlScript.cs:204)
Answer by Kastenessen · Dec 23, 2015 at 11:18 PM
I got it lol. Don't know why it didn't work before except i was using the same variable for creating the area as well. So all I did was change data to dat so they wouldn't conflict and it now seems to work I think.
Have to test it a bit more. The code i changed is:
blockData dat = bformatter.Deserialize(stream) as blockData;
thats all.
Nah still doesn't work. The data variable is empty for some reason but the file has the right data in it. So its not reading or storing the data in my data variable. I really don't understand this. It should be simple.