Getting error when saving List

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

Getting error when saving List

Post by BTippen »

code:
ES3.Save("antSpeciesList", antSpecies_List);

error message:
NotSupportedException: ES3Type.type is null when trying to create an ES3Type for System.Collections.Generic.List`1[AntSpecies], possibly because the element type is not supported.
ES3Internal.ES3TypeMgr.CreateES3Type (System.Type type, System.Boolean throwException) (at Assets/Plugins/Easy Save 3/Scripts/Types/ES3TypeMgr.cs:124)
ES3Internal.ES3TypeMgr.GetOrCreateES3Type (System.Type type, System.Boolean throwException) (at Assets/Plugins/Easy Save 3/Scripts/Types/ES3TypeMgr.cs:32)
ES3Writer.Write (System.Type type, System.String key, System.Object value) (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:142)
ES3Writer.Write[T] (System.String key, System.Object value) (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:129)
ES3.Save[T] (System.String key, T value, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:106)
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,

As the error suggests, it's likely that your AntSpecies class isn't serialisable (for example because it doesn't have a parameterless constructor).

For information on what types are supported see the Supported Types guide. If your class is serialisable based on the specifications in that guide, please could you show me your AntSpecies class?

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 »

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

[Serializable]
public class AntSpecies
{
private string speciesName;
private string speciesClassification;
private string speciesDifficulty;
private string speciesDescription;


private SerializableColor[] bodyPartColors = new SerializableColor[5];
private int[] statLevels = new int[9];


public AntSpecies(AntBuilder antBuilder)
{
speciesName = antBuilder.SpeciesName;
speciesClassification = antBuilder.SpeciesClassification;
speciesDifficulty = antBuilder.SpeciesDifficulty;
speciesDescription = antBuilder.SpeciesDescription;

for (int i = 0; i < antBuilder.StatLevels.Length; i++)
{
statLevels = antBuilder.StatLevels;
}



for(int i = 0;i < antBuilder.BodyPartColors.Length; i++)
{
bodyPartColors = new SerializableColor(antBuilder.BodyPartColors);
}


}
public AntSpecies(string newSpeciesName, string newSpeciesDifficulty, string newSpeciesDescription, SerializableColor[] newBodyPartColors, int[] newStatLevels)
{
speciesName = newSpeciesName;
speciesDifficulty = newSpeciesDifficulty;
speciesDescription = newSpeciesDescription;

statLevels = newStatLevels;
bodyPartColors = newBodyPartColors;

}


public string SpeciesName
{
get
{
return speciesName;
}
set
{
speciesName = value;
}
}
public string SpeciesClassification
{
get
{
return speciesClassification;
}
set
{
speciesClassification = value;
}
}
public string SpeciesDifficulty
{
get
{
return speciesDifficulty;
}
set
{
speciesDifficulty = value;
}
}
public string SpeciesDescription
{
get
{
return speciesDescription;
}
set
{
speciesDescription = value;
}
}

public SerializableColor[] BodyPartColors
{
get
{
return bodyPartColors;
}
set
{
bodyPartColors = value;
}
}
public int[] StatLevels
{
get
{
return statLevels;
}
set
{
statLevels = value;
}
}


}
BTippen
Posts: 25
Joined: Sun Nov 22, 2020 10:58 am

Re: Getting error when saving List

Post by BTippen »

The reason I purchased this easy save is because I want to get the data i saved using a scripteditor to STreamAsset folder available to me when i try and reload in the data on android. At the moment easy save (your solution above worked btw thank you), saves to a seperate folder outside my game folders which doesn't get included in the build.

here is my datacontroller script:

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

public class DataController
{
private string DATA_FILENAME = "/AntSpeciesList.dat";
private AntSpecies antSpecies;

public void EditorSaveAntSpeciesData(List<AntSpecies> antSpecies_List)
{
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();
/*
if (Application.platform == RuntimePlatform.Android)
{

antSpecies_List = ES3.Load<List<AntSpecies>>("antSpeciesList_ES3");
return antSpecies_List;
}
else
{*/
file = File.Open(Application.streamingAssetsPath + 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;
}
}

[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,

Just to clarify, Easy Save accepts an absolute path, so if you want to save to StreamingAssets then you can do so by providing the path as a parameter. I.e.

ES3.Save("antSpeciesList", antSpecies_List, Application.streamingAssetsPath + DATA_FILENAME);

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 »

But saving is not the problem its loading the data back in on android thats the issue.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Getting error when saving List

Post by Joel »

Ah, I understand. As StreamingAssets doesn't exist on Android, you might want to consider using Resources instead:

https://docs.unity3d.com/ScriptReference/Resources.html
https://docs.moodkie.com/easy-save-3/es ... resources/

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 »

I'll give this a try. Thank you for the support Joel.
BTippen
Posts: 25
Joined: Sun Nov 22, 2020 10:58 am

Re: Getting error when saving List

Post by BTippen »

IM now getting a path cant be found error but the path is correct and the file is there:

FileNotFoundException: File "D:/Unity Games/Recovered/Ants2DRecovered11NOV/Assets/Resources/Data/ES3_AntSpeciesList.byte" could not be found.
ES3.Load[T] (System.String key, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:370)
ES3.Load[T] (System.String key, System.String filePath, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:355)
DataController.ES3_LoadAntSpeciesData () (at Assets/Scripts/DataController.cs:114)
UIMenu.LoadEnemyAnts (System.Int32 index) (at Assets/Scripts/UIMenu.cs:108)
UIMenu.LoadGameVsArmy () (at Assets/Scripts/UIMenu.cs:60)

here is the load method:

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, "/Resources/Data" + ES3_DATA_FILENAME, settings);

return antSpecies_List;
}
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,

When loading from Resources you provide a path relative to the Resource folder. I.e.

ES3.Load<List<AntSpecies>>(ES3_DataKey, "Data/" + ES3_DATA_FILENAME, settings);

For more information please see the examples in the Resources guide:
https://docs.moodkie.com/easy-save-3/es ... resources/

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