Page 1 of 1

LoadInto single GameObjects from array?

Posted: Sat May 30, 2020 11:02 am
by snowdrake
Hi,

I have been trying to figure out whether I can use ES3.LoadInto to reference single GameObjects inside a saved GameObject array to their existing counterparts in the scene?

Basically I was trying to go with the usual persistent gameobject saving and loading approach and every time I kind of got lost along the way and thought of a simpler solution.

So I am currently running this:

Code: Select all

ES3.Save("events", GameObject.FindGameObjectsWithTag("Event"), mapFileName);
And now the question is if I can use something like:

Code: Select all

GameObject[] eventarray;
eventarray = ES3.Load<GameObject[]>("events", mapFileName);

foreach(GameObject obj in eventarray)
{
    //doesnt work obviously because I cant grab a single object from an array while loading
    ES3.LoadInto<GameObject>("events", mapFileName, GameObject.Find(obj.name));
}
...you get the idea. I can't use LoadInto on the array properly and I also can't use LoadInto with data that's already loaded, can I?

I'm pretty sure I got confused along the way and there is a much better solution than what I did but right now my brain won't cooperate.

Re: LoadInto single GameObjects from array?

Posted: Sat May 30, 2020 11:35 am
by snowdrake
I could make a seperate savefile just for the events per map and then use ES3.GetKeys and then iterate through each single key that way, but if possible I don't want to have multiple savefiles per scene.

Re: LoadInto single GameObjects from array?

Posted: Sat May 30, 2020 11:55 am
by Joel
Hi there, and welcome to the forum.

The way you've described will not work as ES3.Load<GameObject[]> will load all of the GameObjects. The only alternative would be to save each GameObject to a different key and use ES3.GetKeys, as you've described.

If you wanted to save them all to the same file, you could prepend something to the tag which makes it clear it's a GameObject. I.e.

Code: Select all

ES3.Save("event_"+gameObject.name, myGameObject, mapFileName);
And then when loading:

Code: Select all

var keys = ES3.GetKeys(mapFileName);

foreach(var key in keys)
	if(key.Contains("event_"))
		ES3.LoadInto<GameObject>(key, mapFileName, GameObject.Find(key.Remove("event_")));
All the best,
Joel

Re: LoadInto single GameObjects from array?

Posted: Sat May 30, 2020 12:42 pm
by snowdrake
Worked like a charm.

Thanks for helping me get out of that snag. Have a great weekend!

For anyone else trying this, the string Remove() method takes 2 integers for starting index and amount of characters deleted, so my solution looks like this now:

Code: Select all

    public void Save()
    {
        Debug.Log("MapProgressSave Saving " + mapFileName);

        ES3.Save<Transform>("playerpos", playerObject.transform, mapFileName);

        GameObject[] eventarray;
        eventarray = GameObject.FindGameObjectsWithTag("Event");
        foreach (GameObject obj in eventarray)
        {
            ES3.Save("event_" + obj.name, obj, mapFileName);
        }
    }

    public void Load()
    {
        if (ES3.FileExists(mapFileName))
        {
            Debug.Log("MapProgressSave Loading " + mapFileName);

            ES3.LoadInto<Transform> ("playerpos", mapFileName, playerObject.transform);

            var keys = ES3.GetKeys(mapFileName);

            foreach (var key in keys)
            {
                if (key.Contains("event_"))
                {
                    ES3.LoadInto<GameObject>(key, mapFileName, GameObject.Find(key.Remove(0, 6))); // remove the "event_" in the name
                }
                    
            }
        }
    }