c# - Serializing to disk sometimes fails on Android, but on iOS works fine (Unity3d) -


i'm using binary formatter , writing disk save game state. on ios works perfectly, no issues @ all.

on android on other hand fails. progress lost, 2 variables lost, 2 others saved, goes bonkers.

what might issue? here code serialize/deserialize:

// path file private static string savefilename = "047.bin";   // deserialization function public globalstate(serializationinfo info, streamingcontext ctxt) {     lastbosskilled = (int)info.getvalue("lastboss", typeof(int));     currentlyselectedspells = (spelltype[])info.getvalue("spells", typeof(spelltype[]));     learnedtalents = (int[])info.getvalue("talents", typeof(int[]));     talentpointsavailable = (int)info.getvalue("talentpoints", typeof(int)); }  //serialization function. public void getobjectdata(serializationinfo info, streamingcontext ctxt) {     info.addvalue("lastboss", lastbosskilled);     info.addvalue("spells", currentlyselectedspells);     info.addvalue("talents", learnedtalents);     info.addvalue("talentpoints", talentpointsavailable); } 

here code loads , saves state:

public void savestate() {     using (streamwriter sw = new streamwriter(application.persistentdatapath + "/" + savefilename)) {         binaryformatter bf = new binaryformatter();          bf.serialize(sw.basestream, this);     } }  public static globalstate loadstate() {     try {         using (streamreader sr = new streamreader(application.persistentdatapath + "/" + savefilename)) {             binaryformatter bf = new binaryformatter();             globalstate result = (globalstate)bf.deserialize(sr.basestream);             return result;         }     } catch (exception e) {       return null;    } } 

and here constructor gets called if load returns null:

private globalstate() {     lastbosskilled = -1;     currentlyselectedspells = new spelltype[] { spelltype.slowheal, spelltype.renew, spelltype.none, spelltype.none };     learnedtalents = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };     talentpointsavailable = 0; } 

issues: lastbosskilled not getting saved, currentlyselectedspells somethimes reason has spell in beginning (i feel lastbosskilled getting parsed currentlyselectedspells), talentpointsavailable not getting saved.

again, on ios works fine. on android after kill app, chance high progress lost. save lot , often.

are closing stream each time flush it?

you must call close ensure data correctly written out underlying stream.

https://msdn.microsoft.com/en-us/library/system.io.streamwriter.close(v=vs.110).aspx


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -