Getting error when saving List

Discussion and help for Easy Save 3
BTippen
Posts: 25
Joined: Sun Nov 22, 2020 10:58 am

Re: Getting error when saving List

Post by BTippen »

this line doesnt work when saving: AssetDatabase.Refresh();

Says it doesnt exist in the current context.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Getting error when saving List

Post by Joel »

Hi there,

Unity's AssetDatabase.Refresh() method only exists in the Editor, so you will need to add 'using UnityEditor' to the top of your script.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
BTippen
Posts: 25
Joined: Sun Nov 22, 2020 10:58 am

Re: Getting error when saving List

Post by BTippen »

OK i got it to find the file. But now the List is full of nulls. Its got the correct number of entries just there is nothing in them.

here is my save line:
private string ES3_DATA_FILENAME = "/ES3_AntSpeciesList.bytes";
private string ES3_DataKey = "antSpeciesList";



ES3.Save(ES3_DataKey, antSpecies_List, Application.dataPath + "/Resources/Data" + ES3_DATA_FILENAME);

here is my load lines:

var settings = new ES3Settings();
settings.location = ES3.Location.Resources;

// Load from a file called "myFile.bytes" in Resources.
List<AntSpecies> antSpecies_List = ES3.Load<List<AntSpecies>>(ES3_DataKey, "Data" + ES3_DATA_FILENAME, settings);
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Getting error when saving List

Post by Joel »

Hi there,

Unfortunately it's not possible for me to replicate this from what you've sent. Please could you private message me a very basic project with a basic scene which replicates this?

Unless the objects are null when you save them, the only other thing I can think is happening is that they're being saved by reference but the references no longer exist.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
BTippen
Posts: 25
Joined: Sun Nov 22, 2020 10:58 am

Re: Getting error when saving List

Post by BTippen »

It will be hard to cut the game up as it is very large now. and ill the parts depend on each other.

here is the datacontroller script. If I use the binaryFormatter to save and load it works but only on a PC not on andriod. However when I use Easy Save it loads the right number for the list but each entry is null.

Can you see any errors in this code?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.Networking;
using UnityEditor;

public class DataController
{
private string DATA_FILENAME = "/AntSpeciesList.dat";
private string ES3_DATA_FILENAME = "/ES3_AntSpeciesList.bytes";
private string ES3_DataKey = "antSpeciesList";
private AntSpecies antSpecies;

public void EditorSaveAntSpeciesData(List<AntSpecies> antSpecies_List)
{
ES3.Save(ES3_DataKey, antSpecies_List, Application.dataPath + "/Resources/Data" + ES3_DATA_FILENAME);
AssetDatabase.Refresh();
//ES3.Save("antSpeciesList", antSpecies_List);
FileStream file = null;
try
{



BinaryFormatter bf = new BinaryFormatter();
file = File.Create(Application.streamingAssetsPath + DATA_FILENAME);

bf.Serialize(file, antSpecies_List);
}
catch (Exception error)
{
if (error != null)
{
Debug.Log("There was a saving error " + error);
}
}
finally
{
if (file != null)
{
file.Close();
}
}

}

public List<AntSpecies> LoadAntSpeciesDataList()
{
FileStream file = null;
//var filePath = Application.streamingAssetsPath + DATA_FILENAME;
//string jsonString;
//List<AntSpecies> antSpecies_List;
try
{
List<AntSpecies> antSpecies_List;
BinaryFormatter bf = new BinaryFormatter();


file = File.Open(Application.dataPath + "/Resources/Data" + DATA_FILENAME, FileMode.Open);
antSpecies_List = bf.Deserialize(file) as List<AntSpecies>;
return antSpecies_List;







}
catch (Exception error)
{
if (error != null)
{
Debug.Log("There was a loading error " + error);

}

}
finally
{
if (file != null)
{
file.Close();
}
}
return null;
}

public List<AntSpecies> ES3_LoadAntSpeciesData()
{
var settings = new ES3Settings();
settings.location = ES3.Location.Resources;

// Load from a file called "myFile.bytes" in Resources.
List<AntSpecies> antSpecies_List = ES3.Load<List<AntSpecies>>(ES3_DataKey, "Data" + ES3_DATA_FILENAME, settings);

return antSpecies_List;
}
}

[System.Serializable]
public class SerializableColor
{
public float _r;
public float _g;
public float _b;
public float _a;

public Color Color
{
get
{
return new Color(_r, _g, _b, _a);
}
set
{
_r = value.r;
_g = value.g;
_b = value.b;
_a = value.a;
}
}

public SerializableColor()
{
// (Optional) Default to white with an empty initialisation
_r = 1f;
_g = 1f;
_b = 1f;
_a = 1f;
}

public SerializableColor(float r, float g, float b, float a = 0f)
{
_r = r;
_g = g;
_b = b;
_a = a;
}

public SerializableColor(Color color)
{
_r = color.r;
_g = color.g;
_b = color.b;
_a = color.a;
}
}
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Getting error when saving List

Post by Joel »

Hi there,

Unfortunately I won't be able to replicate it from that alone. Please could you create a script which I can drop into a new project which replicates your issue?

Also please could you send me the save file so I can see what data is being saved?\

Also just to clarify, have you changed any settings in Window > Easy Save 3 > Settings?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
BTippen
Posts: 25
Joined: Sun Nov 22, 2020 10:58 am

Re: Getting error when saving List

Post by BTippen »

Joel,

I sent you a copy of the file produced. I have checked the location it is saving to and it is correct. It is the saving thats the problem i believe. I know the object being saved is good because the same object works using binaryFormatter. Here is the line of code im using to save (noting in the setting has been changed):

Debug.Log("Saving to: " + Application.dataPath + "/Resources/Data" + ES3_DATA_FILENAME);
ES3.Save<List<AntSpecies>>(ES3_DataKey, antSpecies_List, Application.dataPath + "/Resources/Data" + ES3_DATA_FILENAME);
AssetDatabase.Refresh();
BTippen
Posts: 25
Joined: Sun Nov 22, 2020 10:58 am

Re: Getting error when saving List

Post by BTippen »

here you can see the file i am saving is full:
Easy Save example.png
Easy Save example.png (417.41 KiB) Viewed 1314 times
BTippen
Posts: 25
Joined: Sun Nov 22, 2020 10:58 am

Re: Getting error when saving List

Post by BTippen »

Here is the same file getting loaded again. You can see it hasnt saved the data:
Loadexample.jpg
Loadexample.jpg (333.11 KiB) Viewed 1314 times
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Getting error when saving List

Post by Joel »

Hi there,

The issue is because the your AntSpecies class isn't serialisable based on the specification in Supported Types as it has no serialisable fields, leading to an empty object.

If you add the [ES3Serializable] attribute to the fields of your AntSpecies class then it will become serializable.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply