Saving / Loading a dictionary

Discussion and help for Easy Save 3
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving / Loading a dictionary

Post by Joel »

Hi there,

The Easy Save 3 Game Objects will be created because you're saving and loading a Component (Building), but the GameObject which that Component belongs to no longer exists. Therefore Easy Save has to create a GameObject, because a Component cannot exist without a GameObject to attach it to.

Because of this, you should save and load the GameObjects containing the building scripts first. Once you've right-clicked your prefab and selected "Enable Easy Save for Prefab", you can save the GameObjects containing your buildings using something along the lines of:
// Put buildings into an array.
var buildingGameObjects = new List<GameObject>();
foreach(var building in buildings)
    buildingGameObjects.Add(building.Value.gameObject);
ES3.Save<GameObject>("buildingGameObjects", buildingGameObjects);
And then when you load, before loading your Dictionary, load the GameObjects:
ES3.Load<GameObject>("buildingGameObjects");
Regarding the ArgumentException: Only types of UnityEngine.Component can be written with this method, but argument given is type of Farm error you're getting, this should be fixed in the latest version. If you PM me your invoice number I'll send you over the latest version incase the Asset Store isn't distributing it.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
richardm1985
Posts: 9
Joined: Mon Apr 16, 2018 10:12 am

Re: Saving / Loading a dictionary

Post by richardm1985 »

Thank you so much for your help Joel,

I copied your code but when loading I get:

InvalidCastException: Cannot cast from source type to destination type.

The offending code is your ES3.Load method. I've checked that each object does get saved there;
var buildingGameObjects = new List<GameObject>();
        foreach (var building in buildingPositions.Values)
        {
            buildingGameObjects.Add(building.gameObject);
        }

        foreach(var b in buildingGameObjects)
            Debug.Log("Here is what is saved: " + b);
This is the load method:
ES3.Load<GameObject>("BuildingGameObjects");
Am I suppose to load it into something? I've checked that ES3 prefab component is attached to the prefab too.
I've tried loading it to a List, but I get an error that I am trying to load a building to a list.
richardm1985
Posts: 9
Joined: Mon Apr 16, 2018 10:12 am

Re: Saving / Loading a dictionary

Post by richardm1985 »

I remember reading the docs that saving a gameobject will save the components attached to it? Surely that has to be it else I'd be saving every single attached component, including the transform, the button, the image and anything else.

It's like you said, my errors are from saving the component and not the gameobject. So Should I make my dictionary save <Vector2, GameObject>? Instead of <Vector2, Building>? Because you are right, I'm saving my components and ES3 creates new Game Objects, but I am doing nothing with those new GameObjects. I need to attach the component to that particular loaded GameObject.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving / Loading a dictionary

Post by Joel »

Hi there,

My apologies, the Load call is supposed to be:
ES3.Load<List<GameObject>>("BuildingGameObjects");
This creates the GameObjects with the appropriate instance IDs, so it's not necessary to load them into anything. When you load your Dictionary after loading this List, it will recognise that an instance with the given ID already exists, and will use that instance instead of creating a new one.

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