Saving and Loading Objects From Object Pooler

Discussion and help for Easy Save 3
Post Reply
CoolKid404
Posts: 1
Joined: Tue Jul 09, 2019 2:20 pm

Saving and Loading Objects From Object Pooler

Post by CoolKid404 »

Hello,

Anyone have any advice how I could go about having EasySave manage saving/loading GameObjects that are instantiated via an object pooler? When loaded I would need it to take the saved GameObject and be able to remember information from a script thats attached to it, what its children objects are, and its parent at the time of saving was.

I've tried working with the LoadInto method, but have had little luck with results being all over the place of what it loaded, if it loaded at all... I'll put the code that manages the pooled GameObjects below to see if anyone has any ideas how I can handle the objects within it.

Code: Select all

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


[System.Serializable]
public class ObjectPoolItem{
	public GameObject objectToPool;
	public int amountToPool;
	public bool shouldExpand = true;
}

public class objectPooler : MonoBehaviour {
    //Used for Other Scripts To Grab an Object
	public static objectPooler SharedInstance;
    //List of Everything We have Pooled From Above Class
	public List<GameObject> pooledObjects = new List<GameObject>();
    //Contains the Above Class
	public List<ObjectPoolItem> itemsToPool;


	void Awake(){
		SharedInstance = this;
	}

	// Use this for initialization
	void Start () {
		foreach (ObjectPoolItem item in itemsToPool) {
			for (int i = 0; i < item.amountToPool; i++) {
				GameObject obj = Instantiate (item.objectToPool);
				obj.SetActive (false);
				pooledObjects.Add (obj);

            }
		}
	}

	public GameObject GetPooledObject(string tag){
        //Indexes And Grabs an Inactive Object to Use
		for (int i = 0; i < pooledObjects.Count; i++) {
			if (!pooledObjects [i].activeInHierarchy && pooledObjects[i].tag == tag) {
				return pooledObjects [i];
			}
		}

        ///Out of Objects Inactive Objects To Use - Instantiate a New One And Add It To The List of Pooled Objects
        foreach (ObjectPoolItem item in itemsToPool) {
			if (item.objectToPool.tag == tag) {
				if (item.shouldExpand) {
					GameObject obj = (GameObject)Instantiate (item.objectToPool);
					obj.SetActive (false);
					pooledObjects.Add (obj);
                    return obj;
				}
			}
		}
		return null;
	}
}
User avatar
Joel
Moodkie Staff
Posts: 4824
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Objects From Object Pooler

Post by Joel »

Hi there,

If you are using your own object pooler, you will need to manage references yourself because the reference IDs of the objects you instantiate will not match those in the save data otherwise.

You can find a basic example of integrating Easy Save into an existing object pooler here: https://moodkie.com/forum/viewtopic.php?f=16&t=1438

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