Saving List of Custom Type

Discussion and help for Easy Save 3
Post Reply
MikeyNg
Posts: 11
Joined: Tue Nov 30, 2021 8:16 pm

Saving List of Custom Type

Post by MikeyNg »

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!
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving List of Custom Type

Post by Joel »

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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
MikeyNg
Posts: 11
Joined: Tue Nov 30, 2021 8:16 pm

Re: Saving List of Custom Type

Post by MikeyNg »

Code here:

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;
    }

  
}
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???
MikeyNg
Posts: 11
Joined: Tue Nov 30, 2021 8:16 pm

Re: Saving List of Custom Type

Post by MikeyNg »

I'm just having an empty GameObject with a bunch of prefabs in it. So that seems to be working??
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving List of Custom Type

Post by Joel »

Hi there,

Are you getting any warnings in the console?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
MikeyNg
Posts: 11
Joined: Tue Nov 30, 2021 8:16 pm

Re: Saving List of Custom Type

Post by MikeyNg »

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.
robertstaceyfreeland
Posts: 2
Joined: Wed Apr 27, 2022 3:17 pm

Re: Saving List of Custom Type

Post by robertstaceyfreeland »

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
robertstaceyfreeland
Posts: 2
Joined: Wed Apr 27, 2022 3:17 pm

Re: Saving List of Custom Type

Post by robertstaceyfreeland »

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
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving List of Custom Type

Post by Joel »

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
Hi Robert,

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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply