Saving List of Custom Type
Saving List of Custom Type
I want to save a list of a custom Type. The Type is basically a "shell" that holds two GameObjects and an int. (A GameObject, a temporary GO that fades in, and a cost)
When I load the list, the GOs are gone, but the int is still there. It's also weird because there are 1 or 2 specific GOs that actually do get saved. I haven't figured out why that is either.
I've created the Type in the EasySave window. I do get "This type has no serializable properties." under the Properties area, which seems like it could be a problem.
It feels like it's saving references, and they got lost along the way. How do have ES3 save the actual GO rather than a reference?
Thanks!
When I load the list, the GOs are gone, but the int is still there. It's also weird because there are 1 or 2 specific GOs that actually do get saved. I haven't figured out why that is either.
I've created the Type in the EasySave window. I do get "This type has no serializable properties." under the Properties area, which seems like it could be a problem.
It feels like it's saving references, and they got lost along the way. How do have ES3 save the actual GO rather than a reference?
Thanks!
Re: Saving List of Custom Type
Hi there,
Please could you show me the custom type?
Also fields of Unity reference types will be saved by reference. If you want to save the GameObject by value, you would need to save them (or a List of them) separately.
All the best,
Joel
Please could you show me the custom type?
Also fields of Unity reference types will be saved by reference. If you want to save the GameObject by value, you would need to save them (or a List of them) separately.
All the best,
Joel
Re: Saving List of Custom Type
Code here:
How would I save the values rather than the references?
Also: why would it save like 1 or 2 of the prefabs but not others???
Code: Select all
[System.Serializable]
public class TurretBlueprint
{
[SerializeField]
public GameObject prefab;
[SerializeField]
public GameObject fadeInPrefab;
[SerializeField]
public int cost;
public TurretBlueprint()
{
}
public TurretBlueprint(GameObject prefabGO, GameObject fadeInGO, int costValue)
{
prefab = prefabGO;
fadeInPrefab = fadeInGO;
cost = costValue;
}
}
Also: why would it save like 1 or 2 of the prefabs but not others???
Re: Saving List of Custom Type
I'm just having an empty GameObject with a bunch of prefabs in it. So that seems to be working??
Re: Saving List of Custom Type
Hi there,
Are you getting any warnings in the console?
All the best,
Joel
Are you getting any warnings in the console?
All the best,
Joel
Re: Saving List of Custom Type
Yes, I was having those warnings about references, but not to worry if I was creating it dynamically.
I guess I should have been worried
But it seems to work now by "preloading" the GOs into a different prefab that just sits in the scene.
I guess I should have been worried
But it seems to work now by "preloading" the GOs into a different prefab that just sits in the scene.
-
- Posts: 2
- Joined: Wed Apr 27, 2022 3:17 pm
Re: Saving List of Custom Type
Im having trouble getting this to work. It save the correct item but they are empty. I'm guessing its because they are Vector3???
public class scriptObjectSaveLoad : MonoBehaviour
{
public GameObject[] gamePrefabs;
public List<SaveObject> SaveObjects;
public void Awake()
{
SaveObjects = new List<SaveObject>();
}
public void RegisterPrefab(int PrefabId, Vector3 PrefabPosition,Quaternion PrefabRotation)
{
SaveObject mySaveObject = new SaveObject( );
mySaveObject._PrefabId = PrefabId;
mySaveObject._PrefabPosition = PrefabPosition;
mySaveObject._PrefabRotation = PrefabRotation;
SaveObjects.Add(mySaveObject);
}
public void SavePrefabs()
{
ES3.Save<List<SaveObject>>("SaveObjectList", SaveObjects);
}
public void LoadPrefabs()
{
SaveObjects.Clear();
SaveObjects = ES3.Load<List<SaveObject>>("SaveObjectList");
if (SaveObjects.Count > 0)
{
foreach (var prefab in SaveObjects)
{
InstantiatePrefab(prefab._PrefabId, prefab._PrefabPosition, prefab._PrefabRotation);
}
}
}
public GameObject InstantiatePrefab(int PrefabId, Vector3 PrefabPosition, Quaternion PrefabRotation)
{
var go = Instantiate(gamePrefabs[PrefabId], PrefabPosition, PrefabRotation);
return go;
}
}
}
public class SaveObject
{
public int _PrefabId { get; set; }
public Vector3 _PrefabPosition { get; set; }
public Quaternion _PrefabRotation { get; set; }
}
Here is the save file:
{
"SaveObjectList" : {
"__type" : "System.Collections.Generic.List`1[[SaveObject, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]],mscorlib",
"value" : [
{
},{
},{
},{
}
]
}
}
Thanks for the help in advance
public class scriptObjectSaveLoad : MonoBehaviour
{
public GameObject[] gamePrefabs;
public List<SaveObject> SaveObjects;
public void Awake()
{
SaveObjects = new List<SaveObject>();
}
public void RegisterPrefab(int PrefabId, Vector3 PrefabPosition,Quaternion PrefabRotation)
{
SaveObject mySaveObject = new SaveObject( );
mySaveObject._PrefabId = PrefabId;
mySaveObject._PrefabPosition = PrefabPosition;
mySaveObject._PrefabRotation = PrefabRotation;
SaveObjects.Add(mySaveObject);
}
public void SavePrefabs()
{
ES3.Save<List<SaveObject>>("SaveObjectList", SaveObjects);
}
public void LoadPrefabs()
{
SaveObjects.Clear();
SaveObjects = ES3.Load<List<SaveObject>>("SaveObjectList");
if (SaveObjects.Count > 0)
{
foreach (var prefab in SaveObjects)
{
InstantiatePrefab(prefab._PrefabId, prefab._PrefabPosition, prefab._PrefabRotation);
}
}
}
public GameObject InstantiatePrefab(int PrefabId, Vector3 PrefabPosition, Quaternion PrefabRotation)
{
var go = Instantiate(gamePrefabs[PrefabId], PrefabPosition, PrefabRotation);
return go;
}
}
}
public class SaveObject
{
public int _PrefabId { get; set; }
public Vector3 _PrefabPosition { get; set; }
public Quaternion _PrefabRotation { get; set; }
}
Here is the save file:
{
"SaveObjectList" : {
"__type" : "System.Collections.Generic.List`1[[SaveObject, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]],mscorlib",
"value" : [
{
},{
},{
},{
}
]
}
}
Thanks for the help in advance
-
- Posts: 2
- Joined: Wed Apr 27, 2022 3:17 pm
Re: Saving List of Custom Type
I think I understand where I went wrong. Obviously Es3 won't accomodate my type. So I broke it up into the components that are accepted.
ps Great Product
Thanks
Robert Freeland
ps Great Product
Thanks
Robert Freeland
Re: Saving List of Custom Type
Hi Robert,robertstaceyfreeland wrote: ↑Wed Apr 27, 2022 3:48 pm I think I understand where I went wrong. Obviously Es3 won't accomodate my type. So I broke it up into the components that are accepted.
ps Great Product
Thanks
Robert Freeland
Only fields are saved automatically. Your SaveObject class contains properties, not fields, so none of them will be serialised. Adding the [ES3Serializable] attribute to them will allow them to be serialised. For more information please see this guide:
https://docs.moodkie.com/easy-save-3/es ... -is-saved/
All the best,
Joel